Home  >  Article  >  Backend Development  >  PHP code example to limit file download speed_PHP tutorial

PHP code example to limit file download speed_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:16856browse

Sometimes you will need to slow down the speed of downloading files for some purpose, for example, you want to implement a file download progress bar function. The biggest benefit of limiting download speed is to save bandwidth and avoid network congestion caused by excessive instantaneous traffic.

php实现限制文件下载速度的代码实例  帮客之家

Analysis: The above example limits the file download speed to 20.5kb/s, that is, only sends a 20.5kb file stream to the client per second until the entire file is sent. If there is no such limit, the files will be sent to the client together in the form of a stream. What will happen if there are as many as there are sent? If the file size is 2m, then 2m of data stream will be transmitted at once, which may cause The network is blocked and the execution of the script is interrupted. This download method cannot be used in practical applications.

In terms of technology, first add a header file, declare Content-Type as application/octet-stream, indicating that the request will be sent as a stream, and declare Content-Length, which declares the size of the file stream. Flush() is used in the code. The flush function is to refresh the buffer of the php program and realize dynamic print output.

The above code, after clever use, can realize the function of the client displaying the file download progress bar. If you are interested, you may wish to try it.

   Note: For more exciting tutorials, please pay attention to Bangkejia's Tutorials channel,

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752069.htmlTechArticleSometimes you will ask to slow down the download speed for some purpose, for example, you want to achieve File download progress bar function. The biggest advantage of limiting download speed is to save bandwidth and avoid...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn