2010-07-05 62 views
0
function Ifurl($subject) 
    { 
    $pattern = "/http:\/\//"; 
    $regex = preg_match_all($pattern, $subject, $array); 
    if ($regex == 1) 
    { 
     return true; //true that it exist 
    } 
    else 
    { 
     return false; 
    } 
} 

    function get_start_point($env_http_range) 
{ 
    //Calculate start point 
    $range = $env_http_range; // usually set to $_SERVER['HTTP_RANGE'] 
    if ($range != '') 
    { 
     $p1 = strpos($range, "bytes="); 
     $p2 = strpos($range, "-"); 
     $offset = substr($range, $p1+6, $p2-($p1+6)); 
    } else { 
     $offset = 0; 
    } 


    return $offset; 
} 


    function Output($url,$range,$range2) //cant record output 8192 not full amount 

    { 
    $opts = array('http'=>array('method'=>"GET",'header'=>"Range: bytes=$range-$range2")); 

    $context = stream_context_create($opts); 
    $fp  = fopen($url, 'r', false, $context); 
    while(!feof($fp) && !connection_aborted()) 
    { 
    $data_transferred += 1024 * 8; 
    fpassthru($fp); 
    } 
    $status = (!connection_aborted()); 
    log_this($data_transferred); 
    fclose($httphandle); 
    } 

    function log_this($line) 
{ 
    $filename = 'download_log.txt'; 
    $handle = fopen($filename, 'a'); 
    fwrite($handle, $line . "\n"); 
} 


function url($url) 
{ 
    $define_url = Ifurl($url); //define if the url is true! 
    if ($define_url == 1) 
    { 
     $filename = basename($url); //define the filename of the file 
     $httphandle = fopen($url, "r"); //open the file and read the information 
     $headers  = stream_get_meta_data($httphandle); 
     $filesize = $headers['wrapper_data'][6]; //define the file conent size 
     $filecontent = $headers['wrapper_data'][8]; //get the file content 
     $pattern  = "/(\d+)/"; //get all filesize dg 
     preg_match_all($pattern, $filesize, $array); 
     $filesize = $array[0][0]; //get the filesize; 
     $returnarray = array("Filename" => $filename,"Filesize" => $filesize, "Fileurl" => $url,"Filecontent" => $filecontent); 
     return $returnarray; 
    } 
    else 
    { 

     return "Unable"; //unable to make request! 
    } 

} 



function XD($url) 
{ 

$graburl  = url($url); 
$filename = $graburl['Filename']; 
$filesize = $graburl['Filesize']; 
$filurl  = $graburl['Fileurl']; 
$filecontent = $graburl['Filecontent']; 

/////////////////////////////////////////// 
set_magic_quotes_runtime(0); 
ini_set('output_buffering', 'Off'); 
ob_end_clean(); 
ini_set('zlib.output_compression', 'Off'); 
ignore_user_abort(true); 


//////////////////////////////////////////// 


@header("Cache-Control:"); 
@header("Cache-Control: public"); 
@header("Content-Type: application/octet-stream"); 
@header("Content-Disposition: attachment; filename=".$filename); 
@header("Accept-Ranges: bytes"); 


    $log = get_start_point($_SERVER['HTTP_RANGE']); 
    log_this($log); 

    if(isset($_SERVER['HTTP_RANGE'])) 
    { 
    list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); 
    list($range1,$range2) = explode("-", $range); 
    if ($range2 == '' or $range2 < 0 or $range2 >= $filesize) { 
     $range2 = $filesize -1; 
    }; 
    if ($range1 < 0 or $range1 > $range2) { 
     $range1 = 0; 
    } 
    $new_length = $range2 - $range1 + 1; 

    @header("HTTP/1.1 206 Partial Content"); 
    @header("Content-Range: bytes $range1-$range2/$filesize"); 
    @header("Content-Length: $new_length"); 


    Output($url,$range1,$range2); 

} 
else { 
    @header("Content-Length: ".$filesize); 

     $httphandle = fopen($url, "r"); //open the file and read the information 
     while (!feof($httphandle) && !connection_aborted()) 
     { 
     $data_transferred += 1024 * 8; 
     echo fread($httphandle, 1024 * 8); 
      flush(); 
     ob_end_flush(); 
     } 
     $status = (!connection_aborted()); 
     log_this($data_transferred); 
     fclose($httphandle); 
} 

    } 


XD('http://rarlabs.com/rar/wrar393.exe') 

我想恢復下載並使用緩衝區記錄已經下載的字節代碼很好用!但我無法記錄從用戶下載的全部字節數,輸出函數與服務器通信以發送用戶已離開的字節我用一個文本文件作爲測試記錄它,當用戶下載時記錄字節,用戶下載一個將其保存到文本文件但唯一的問題是當用戶調用函數輸出日誌不記錄多少字節我嘗試使用fread,但代碼進入choas如何可以記錄用戶的總數下載的字節數量?如何判斷髮送了多少個字節?

+0

我建議你使用句號。我不明白這個問題。這是什麼意思,「當用戶打電話他功能?」? – Artefacto 2010-07-05 20:10:07

+0

函數輸出($ url,$ range,$ range2) – Saxtor 2010-07-06 00:39:23

回答

0

你看着curl擴展?

+0

curl我想知道如何記錄用戶多少字節的用戶downlaoded – Saxtor 2010-07-06 00:39:43

+0

我很確定curl可以跟蹤傳輸的字節。去閱讀文檔。 – Kalium 2010-07-06 00:40:24

+0

我確實可以解釋一下如何在用戶下載文件時跟蹤字節 – Saxtor 2010-07-06 02:19:30

相關問題