PHP使用带宽计数器将远程文件下载到客户端

时间:2014-09-07 11:52:16

标签: php networking download resume-download

我有一个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,如果达到它。下载不能再进行下载了。

0 个答案:

没有答案