Home  >  Article  >  Backend Development  >  Limit file download speed php code

Limit file download speed php code

WBOY
WBOYOriginal
2016-07-25 08:45:15981browse

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn