PHP file Download (can limit download speed) Implementation Code _php Tutorial

Source: Internet
Author: User
Tags fread
In PHP file download will take advantage of the header fopen fread three main functions, while there are some auxiliary functions such as determining the existence of the file file_exists is_file, such as functions, let's look at a file download can limit the download speed instance

In PHP tutorial file download will take advantage of the header fopen fread three main functions, while there are some auxiliary functions such as determining the existence of the file file_exists Is_file, and so on, let's look at a file download can limit the download speed instance

*/
$file = "Test.mp3"; File to BES send to the client
$speed = 8.5; 8,5 kb/s Download Rate limit
if (file_exists ($file) && is_file ($file)) {
Header ("Cache-control:private");
Header ("Content-type:application/octet-stream");
Header ("Content-length:". FileSize ($file));
Header ("Content-disposition:filename= $file". "%20");
Flush ();
$FD = fopen ($file, "R");
while (!feof ($FD)) {
Echo fread ($FD, round ($speed *1024));
Flush ();
Sleep (1);
}
Fclose ($FD);
}

/*
Flush
Flush function Refreshes PHP program's buffering implementation echo dynamic output
The result of this function is that the page continually displays the data of the Echo output
for ($i =10; $i >0; $i--)
{
echo $i. '
';
Ob_flush ();
Flush ();
Sleep (1);
}
Ob_end_flush ();

Sleep
The sleep () function delays code execution for several seconds.
Header
The header () function sends the original HTTP header to the client.

It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):


FileSize Get File size
Fread read the contents of a file opened by fopen


http://www.bkjia.com/PHPjc/444821.html www.bkjia.com true http://www.bkjia.com/PHPjc/444821.html techarticle in php file download will take advantage of the header fopen fread three main functions, while there are some auxiliary functions such as determining the existence of the file file_exists is_file, such as functions, let's look at a file ...

  • Related Article

    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.