Maison  >  Article  >  développement back-end  >  php限制文件下载速度

php限制文件下载速度

WBOY
WBOYoriginal
2016-07-25 08:46:07835parcourir
  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. }
  28. else {
  29. die('Error: The file '.$local_file.' does not exist!');
  30. }
复制代码

下载速度, php


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn