当前位置:Gxlcms > PHP教程 > 限制文件下载速度php代码

限制文件下载速度php代码

时间:2021-07-01 10:21:17 帮助过:4人阅读

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

人气教程排行