I need to create an handy file down loader which will count the amount of bytes downloaded and stop when it has exceed a preset limit.
i need to mirror some files but i only have 7 gb per moth of bandwidth and i dont want to exceed the limit.
Example limits can be in bytes or number of files, each user has their own limit, as well as a limit for Download Quota itself. So if you set a limit of 2 gigabytes for Download Quota, downloads stop at 2 gigabytes, even if you have 3 users with a limit of 1 gigabyte each.
if ($range)
{
//pass client Range header to rapidshare
// _insert($range);
$cookie .= "\r\nRange: $range";
$multipart = true;
header("X-UR-RANGE-Range: $range");
}
//octet-stream + attachment => client always stores file
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' .
$fn . '"');
//always included so clients know this script supports resuming
header("Accept-Ranges: bytes");
//awful hack to pass rapidshare the premium cookie
$user_agent = ini_get("user_agent");
ini_set("user_agent", $user_agent . "\r\nCookie: enc=$cookie");
$httphandle = fopen($url, "r");
$headers = stream_get_meta_data($httphandle);
//let's check the return header of rapidshare for range / length indicators
//we'll just pass these to the client
foreach ($headers["wrapper_data"] as $header)
{
$header = trim($header);
if (substr(strtolower($header), 0, strlen("content-range")) ==
"content-range")
{
// _insert($range);
header($header);
header("X-RS-RANGE-" . $header);
$multipart = true; //content-range indicates partial download
} elseif (substr(strtolower($header), 0, strlen("Content-Length")) ==
"content-length")
{
// _insert($range);
header($header);
header("X-RS-CL-" . $header);
}
}
//now show the client he has a partial download
if ($multipart) header('HTTP/1.1 206 Partial Content');
flush();
$download_rate = 100;
while (!feof($httphandle))
{
// send the current file part to the browser
$var_stat = fread($httphandle, round($download_rate * 1024));
$var12 = strlen($var_stat);
//////////////////////////////////
echo $var_stat;
/////////////////////////////////
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}