欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php控制文件下载速度

程序员文章站 2022-04-14 23:36:53
...
  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