我有一个 download.php 远程文件的代码:
function _downloadIt($url,$size,$filename,$user='',$pass='',$cookie='')
{
$filesize = $size;
$filename = $filename;
$directlink = $url;
$link = $directlink;
$link = str_replace(" ","%20",$link);
if(!$link) {
sleep(15);
header("HTTP/1.1 404 Link Error");
}
$range = '';
if (isset($_SERVER['HTTP_RANGE'])) {
$range = substr($_SERVER['HTTP_RANGE'], 6);
}
$port = 80;
$schema = parse_url(trim($link));
$host= $schema['host'];
$scheme = "http://";
$gach = explode("/", $link);
list($path1, $path) = explode($gach[2], $link);
if(isset($schema['port'])) $port = $schema['port'];
elseif ($schema['scheme'] == 'https') {
$scheme = "ssl://";
$port = 443;
}
if ($scheme != "ssl://") {
$scheme = "";
}
$hosts = $scheme . $host . ':' . $port;
$fp = @stream_socket_client ($hosts, $errno, $errstr, 120, STREAM_CLIENT_CONNECT );
if (!$fp) {
sleep(15);
header("HTTP/1.1 404 Not Found");
die ("FP Error");
}
$data = "GET {$path} HTTP/1.1\r\n";
$data .= "User-Agent: "."SiiWulyo/0.1"."\r\n";
$data .= "Host: {$host}\r\n";
$data .= "Accept: */*\r\n";
$data .= $cookie ? "Cookie: ".$cookie."\r\n" : '';
if ($user && $pass)
{
$data .= "Authorization: Basic " . base64_encode("{$user}:{$pass}") . "\r\n";
}
if (!empty($range)) $data .= "Range: bytes={$range}\r\n";
$data .= "Connection: Close\r\n\r\n";
@stream_set_timeout($fp, 2);
fputs($fp, $data);
fflush($fp);
$header = '';
do {
$header .= stream_get_line($fp, 512);
}
while (strpos($header, "\r\n\r\n" ) === false);
// Must be fresh start
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: bytes");
if(stristr($header,"TTP/1.0 200 OK") || stristr($header,"TTP/1.1 200 OK")) {
$filesize = trim (cut_str ($header, "Content-Length:", "\n" ));
if(stristr($header,"filename")) {
$filename = trim (cut_str ( $header, "filename", "\n" ) );
$filename = preg_replace("/(\"\;\?\=|\"|=|\*|UTF-8|\')/","",$filename);
}
//die($filesize);
header("HTTP/1.1 200 OK");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Length: {$filesize}");
}
elseif(stristr($header,"TTP/1.1 206") || stristr($header,"TTP/1.0 206")) {
sleep(1);
$new_length = trim (cut_str ($header, "Content-Length:", "\n" ));
$new_range = trim (cut_str ($header, "Content-Range:", "\n" ));
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: $new_range");
}
elseif(stristr($header,"TTP/1.1 302 Moved")){
return _downloadIt(trim(cut_str ( $header, "Location:", "\n" ) ),$size, $filename, $user, $pass,$cookie);
//kembali lagi gan
}
else {
sleep(10);
header("HTTP/1.1 404 Not Found header");
die ($header);
}
$tmp = explode("\r\n\r\n", $header);
if ($tmp[1]) {
print $tmp[1];
}
while (!feof($fp) && (connection_status()==0)) {
$recv = @stream_get_line($fp, 512);
@print $recv;
@flush();
@ob_flush();
}
fclose($fp);
exit;
}
这就是代码,它非常适合远程直接链接。我想知道的是:如何使该代码计算已经下载到客户端的字节或带宽?
我的意思是这样,一个由多个客户端/用户下载的文件。前任。10 客户。文件大小为 50mb。只有 4 个客户端/用户下载完成了 100%,而另外一个仅完成了 50% 或 40%。而已。
我想知道如何计算传输到客户端的字节数?就像计数器下载了多少次一样。但是现在的问题是该文件已经使用了多少字节/ bw。我的最终目的是限制可以使用多少带宽。例如在 1000MB,如果它达到它。不能再下载了。