Thursday, November 13, 2014

Get Acknowledgement of How to Download File in PHP with Speed Limit

We have seen that, in some website, we can only download files at a certain download limit even though we have a require Internet connection. This is a cause due to the file download has been set with a speed limit to download. Check out the below tutorial that how we can achieve this using PHP. Use following code snippet in your code to execute it.


// filename that the user gets for downloading
$download_file = 'file-to-download.zip';
// set the download rate limit
$download_rate = 20.5; //=> 20,5 kb/s

header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($download_file));
header('Content-Disposition: filename='.$download_file);
flush();
// open file stream
$file = fopen($download_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(1);
}
fclose($file);}

?>

Above code will set a speed limit to 20.5kb/s for the file download. 

0 comments:

Post a Comment