Limit file download speed PHP code

Source: Internet
Author: User

The following code to implement PHP limit download speed:

  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. ?>
Copy Code
Download speed, PHP
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.