Php code restricting file download speed

Source: Internet
Author: User
This article mainly introduces php code that limits the speed of file download, with detailed code analysis, which has some reference value, for more information, see. sometimes you may 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. This article will share with you how to use php code to limit the download speed of files.
First, let's look at the code that limits the download speed of files using php:

<? Php // specifies the local file that will be sent to the client. '; // the file name must be 'your-download-name.zip'; // sets the download rate (=> 31.2 kb/s) $ download_rate = 31.2; if (file_exists ($ local_file) & is_file ($ local_file) {header ('cache-control: private '); // send headers header ('content-Type: application/octet-stream '); header ('content-Length :'. filesize ($ local_file); header ('content-Disposition: filename = '. $ download_file); flush ();/ /Refresh content $ file = fopen ($ local_file, "r"); while (! Feof ($ file) {print fread ($ file, round ($ download_rate * 1024); // send the current part of the file to the viewer flush (); // flush content output to sleep (1) on the browser side; // The terminal continues after 1 second} fclose ($ file); // close the file stream} else {die ('Error: file '. $ local_file. 'does not exist! ');}

Perform some analysis on the above code:
1. set the file download speed to 31.2kb/s.That is, only a file stream of 5kb 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, and how much 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.
2. Technical aspectsFirst, 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, that is, the file stream size. The flush () function is used in the code to refresh the buffer of the php program and implement print dynamic output.

Note that the above code can also be used to display the file download progress bar on the client. if you are interested, you can try it.

The above implementation of the php file download speed limit function, I hope this article will be helpful for everyone's php program design.

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.