A simple and effective way to limit the rate at which a file is downloaded. Good for user level file transfers. If you still want to allow guests to download files at a slower speed this might be the answer.
Snippet:
-
<?
-
// speed is in kB/s
-
// $dst_filename is what the user gets
-
// $src_filename is the file on the server
-
// http://thedevnet.com/php/snippets/speed-limit-download-of-file/
-
-
function dl_speedcap($src_filename, $dst_filename, $speed)
-
{
-
// local file
-
$local_file = $src_filename;
-
// filename that the user gets
-
$download_file = $dst_filename;
-
-
// set the speed
-
$download_rate = $speed;
-
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!');
-
}
-
}
-
?>
Example:
-
<?
-
dl_speedcap("myfile.zip", "theirfile.zip", "8");
-
?>
GD Star Rating
loading...
Originally posted 2009-10-17 02:55:08.
Popularity: 12%
Posted by irbobo @ 8 February 2010
7:58 am
perhaps a modification that would allow slower media downloading might be possible with this snippet
loading...