Php implementation download speed limit example sharing _ php instance

Source: Internet
Author: User
This article describes how to limit the download speed of php. For more information, see The Code is as follows:


// Local file that shocould be sent to the client
$ Local_file = 'test-file.zip ';

// Filename that the user gets as default
$ Download_file = 'your-download-name.zip ';

// Set the download rate limit (=> 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 file stream
$ File = fopen ($ local_file, "r ");

While (! Feof ($ file )){

// Send the current file part to the browser
Print fread ($ file, round ($ download_rate * 1024 ));

// Flush the content to the browser
Flush ();

// Sleep one second
Sleep (1 );
}

// Close file stream
Fclose ($ file );

}

Else {
Die ('error: The file'. $ local_file. 'does not exist! ');
}

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.