Php code example that limits the speed of File Download

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn. Sometimes you may want to slow down the download speed for some purpose, for example, you want to implement the file download progress bar function. The biggest advantage of limiting the download speed is to save bandwidth and avoid network congestion caused by excessive instantaneous traffic.
// The local file that will be sent to the client
$ Local_file = 'test-file.zip ';
// File name
$ Download_file = 'your-download-name.zip ';
// Set the download rate (=> 20, 5 kb/s)
$ Download_rate = 20.5;
If (file_exists ($ local_file) & is_file ($ local_file )){
// Send headers
Header ('cache-control: private ');
Header ('content-Type: application/octet-stream ');
Header ('content-Length: '. filesize ($ local_file ));
Header ('content-Disposition: filename = '. $ download_file );
// Flush content
Flush ();
// Open the file stream
$ File = fopen ($ local_file, "r ");
While (! Feof ($ file )){
// Send the current part of the file to the viewer
Print fread ($ file, round ($ download_rate * 1024 ));
// Flush the content to the browser
Flush ();
Ob_flush (); // prevents the caching mechanism of PHP or web server from affecting the output.
// Terminal continues in 1 second
Sleep (1 );
}
// Close the file stream
Fclose ($ file );
}
Else {
Die ('error: File '. $ local_file.' does not exist! ');
}
Analysis: The above example limits the file download speed to kb/s, that is, only a kb file stream is sent to the client every second until the complete file is sent. If there is no such restriction, the file will be sent together to the client in the form of a stream. How many messages will be sent? What will happen? If the file size is 2 MB, the 2 mb data stream will be transferred at once, which may cause network congestion and Interrupt script execution. This download method cannot be used in practical applications.

Technically, first add the header file and declare Content-Type as application/octet-stream. This indicates that the request will be sent as a stream and the Content-Length is declared, the file stream size is declared. The flush () function is used in the code to refresh the buffer of the php program and implement print dynamic output.

The above code can be cleverly used to display the file download progress bar on the client. If you are interested, try it.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.