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

php控制文件下载速度

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

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

下载速度, php

人气教程排行