首页  >  文章  >  后端开发  >  PHP限制文件下载的速度

PHP限制文件下载的速度

WBOY
WBOY原创
2016-07-23 08:54:49755浏览
  1. // local file that should be send to the client
  2. $local_file = 'test-file.zip';
  3. // filename that the user gets as default
  4. $download_file = 'your-download-name.zip';
  5. // set the download rate limit (=> 20,5 kb/s)
  6. $download_rate = 20.5;
  7. if(file_exists($local_file) && is_file($local_file)) {
  8. // send headers
  9. header('Cache-control: private');
  10. header('Content-Type: application/octet-stream');
  11. header('Content-Length: '.filesize($local_file));
  12. header('Content-Disposition: filename='.$download_file);
  13. // flush content
  14. flush();
  15. // open file stream
  16. $file = fopen($local_file, "r");
  17. while(!feof($file)) {
  18. // send the current file part to the browser
  19. print fread($file, round($download_rate * 1024));
  20. // flush the content to the browser
  21. flush();
  22. // sleep one second
  23. sleep(1);
  24. }
  25. // close file stream
  26. fclose($file);}
  27. else {
  28. die('Error: The file '.$local_file.' does not exist!');
  29. }
  30. ?>
复制代码

PHP


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn