首頁  >  文章  >  後端開發  >  限制文件下载速度php代码

限制文件下载速度php代码

WBOY
WBOY原創
2016-07-25 08:45:15970瀏覽

如下代码实现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


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn