PHP implementation limit file download speed Code instance

Source: Internet
Author: User
Tags file size flush

Sometimes you may want to slow down the download file for some reason, such as you would like to implement the file download progress bar function. The biggest advantage of limiting download speed is that it saves bandwidth and avoids the network jam caused by excessive instantaneous traffic.

Analysis: The above example limits file download speed to 20.5kb/s, that is, sending only 20.5kb of file streams per second to the client until the entire file is sent. If this limit is not available, then the file will be sent to the client in the form of a stream, how much will be sent, what happens? If the file size is 2m, then all of a sudden to the 2m data circulated to the past, this will likely lead to network congestion and interrupt the execution of the script, this download method can not be used in practical applications.

Technically, you first add a header file, declare Content-type as Application/octet-stream, indicate that the request will be sent as a stream, and declare content-length, which declares the file flow size. The flush () is used in the code, and the flush function is to refresh the buffer of the PHP program and realize the print dynamic output.

The above code, after clever use, can realize the client to display file download progress bar function, interested words may wish to try.

Note : Please pay attention to the triple tutorial channel for more wonderful tutorials,

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.