限制文件下载速度php代码

WBOY
Libérer: 2016-07-25 08:45:15
original
974 Les gens l'ont consulté

如下代码实现php限制下载速度:

  1. /* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
  2. /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
  3. $download_rate = 10.20;
  4. $download_file = 'download-file.zip';
  5. $target_file = 'target-file.zip';
  6. if(file_exists($download_file)){
  7. /* headers */
  8. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  9. header('Cache-control: private');
  10. header('Content-Type: application/octet-stream');
  11. header('Content-Length: '.filesize($download_file));
  12. header('Content-Disposition: filename='.$target_file);
  13. /* flush content */
  14. flush();
  15. /* open file */
  16. $fh = @fopen($download_file, 'r');
  17. while(!feof($fh)){
  18. /* send only current part of the file to browser */
  19. print fread($fh, round($download_rate * 1024));
  20. /* flush the content to the browser */
  21. flush();
  22. /* sleep for 1 sec */
  23. sleep(1);
  24. }
  25. /* close file */
  26. @fclose($fh);
  27. }else{
  28. die('Fatal error: the '.$download_file.' file does not exist!');
  29. }
  30. ?>
复制代码

下载速度, php


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!