0

I have a code for download.php remote file:

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;
}

That's the code, it work perfectly for remote direct link. What I want to know is: How can I make that code count the bytes or bandwidth already download to client?

I mean like this, a file downloaded by several client/user. ex. 10 client. The filesize is 50mb. And only 4 client/user download completed 100% and another just 50% or 40%. That's it.

I want to know how to count that how many bytes transferred to the client? Just like counter how many times downloaded. But the problem now is how many bytes / bw already used for that files. My final purpose is making limit of how many bandwidth can used. For example at 1000MB, if it reach it. Downloads can't be done anymore than.

msrd0
  • 7,816
  • 9
  • 47
  • 82

0 Answers0