Home  >  Article  >  Backend Development  >  PHP limits file download speed

PHP limits file download speed

WBOY
WBOYOriginal
2016-07-23 08:54:49755browse
  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


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