PHP limits the speed of file download

Source: Internet
Author: User
PHP limits the speed of file download

  1. // Local file that shocould be sent 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

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.