How to limit the download speed in PHP

Source: Internet
Author: User
When you are doing O & M, you will often encounter a problem, that is, someone downloads something in the office, affecting Internet access and office work. In the same case, if you encounter a problem on the server, you may often encounter a problem, that is, someone downloads something in the office, affecting Internet access and office work. If the same problem occurs on the server, it is estimated that the boss will get angry and things will get worse ...... Today, I would like to recommend several lines of code to limit the network speed of PHP. I hope to help you.
 
[Code] [PHP] Code view sourceprint?
01 // Local file that shocould be sent to the client
02 Comment ';
03
04 // Filename that the user gets as default
05 Unzip download_file+'your-download-name.zip ';
06
07 // Set the download rate limit (=> 20, 5 kb/s)
08 $ Download_rate = 20.5;
09
10 If (file_exists ($ local_file) & is_file ($ local_file )){
11
12 // Send headers
13 Header ('cache-control: private ');
14 Header ('content-Type: application/octet-stream ');
15 Header ('content-Length: '. filesize ($ local_file ));
16 Header ('content-Disposition: filename = '. $ download_file );
17
18 // Flush content
19 Flush ();
20
21 // Open file stream
22 $ File = fopen ($ local_file, "r ");
23
24 While (! Feof ($ file )){
25
26 // Send the current file part to the browser
27 Printfread ($ file, round ($ download_rate * 1024 ));
28
29 // Flush the content to the browser
30 Flush ();
31
32 // Sleep one second
33 Sleep (1 );
34 }
35
36 // Close file stream
37 Fclose ($ file );
38
39 }
40
41 Else {
42 Die ('error

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.