PDA

View Full Version : Function to force download at certain speed


frosty1433
01-10-2007, 03:04 PM
function force_download($file, $filename="", $directory="/", $downloadspeed="10")
{
if ($directory != "" || substr($directory, -1, 1) != "/")
{
$directory = $directory . "/";
}
if ($filename == "")
{
$filename = $file;
}
$filepath = $directory . $file;
unset($file, $directory);
if (!$file = fopen($filepath, 'r'))
{
return false;
}
else
{
header("Cache-control: private");
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($filepath));
header('Content-Disposition: attachment; filename=' . $filename);
while (!feof($file))
{
echo fread($file, $downloadspeed * 1024 * 8);
flush();
sleep(1);
}
@fclose($file);
}
return true;
}

Credit:
http://www.phpnerds.com/article/speed-limiting-file-downloads

firepages
01-11-2007, 03:37 PM
why would you want to slow down the download ... and your server ? if there is a reason (and there may well be)... what is it ?

marek_mar
01-11-2007, 07:49 PM
For example if your server doesn't have all the upload speed you want you can allow some bandwidth for others. If your site depends on it, you could let paying users download faster.
You asked for reasons, not if it's viable in PHP. :p

firepages
01-12-2007, 01:10 AM
... you could let paying users download faster.

fair enough.. never thought of that one.

frosty1433
01-19-2007, 08:18 AM
Well, I've discovered that this script is downloading slightly faster than it should be, but not by much. (Maybe my internet is just too fast? :p)

mattd8752
01-21-2007, 02:09 PM
If your looking for another reason, you could make it so opening images from on other servers is very slow. (And forces them to be taken as apps, forcing sites to download your images and host them on their site, not hotlink, by checking if the image is being called by your server.)

Velox Letum
01-23-2007, 08:30 PM
If your looking for another reason, you could make it so opening images from on other servers is very slow. (And forces them to be taken as apps, forcing sites to download your images and host them on their site, not hotlink, by checking if the image is being called by your server.)

That's a rather contrived use. If you want to block hotlinking, there's much easier ways to do so, such as rewrite rules.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?yoursite\.com [NC]
RewriteRule \.(gif|jpe?g|png)$ - [F,NC,L]

frosty1433
01-24-2007, 12:45 AM
An idea I had for this is to right a code to moniter how many users are on your server, and divide the download speed evenly to everyone, to prevent the server from overloading. That was my original idea with this... (I have no idea if that would work though, but it sounds nice :P)

thiscolddecembe
08-12-2007, 12:13 AM
...$online_users = mysql_num_rows($result);
//total bandwidth you want to use (downloading)
$totalb = 2000;
function force_download($file, $filename="", $directory="/", $downloadspeed="$totalb")


Something like that? Not sure about specifics.