限制文件下载速度php代码

WBOY
Freigeben: 2016-07-25 08:45:15
Original
975 Leute haben es durchsucht

如下代码实现php限制下载速度:

  1. /* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
  2. /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
  3. $download_rate = 10.20;
  4. $download_file = 'download-file.zip';
  5. $target_file = 'target-file.zip';
  6. if(file_exists($download_file)){
  7. /* headers */
  8. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  9. header('Cache-control: private');
  10. header('Content-Type: application/octet-stream');
  11. header('Content-Length: '.filesize($download_file));
  12. header('Content-Disposition: filename='.$target_file);
  13. /* flush content */
  14. flush();
  15. /* open file */
  16. $fh = @fopen($download_file, 'r');
  17. while(!feof($fh)){
  18. /* send only current part of the file to browser */
  19. print fread($fh, round($download_rate * 1024));
  20. /* flush the content to the browser */
  21. flush();
  22. /* sleep for 1 sec */
  23. sleep(1);
  24. }
  25. /* close file */
  26. @fclose($fh);
  27. }else{
  28. die('Fatal error: the '.$download_file.' file does not exist!');
  29. }
  30. ?>
复制代码

下载速度, php


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!