Heim  >  Artikel  >  Backend-Entwicklung  >  php限制文件下载速度

php限制文件下载速度

WBOY
WBOYOriginal
2016-07-25 08:46:07833Durchsuche
  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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn