2010-07-05 3 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

+0

함수 출력 ($ url, $ range, $ range2) – Saxtor

답변

0

당신이 곱슬 곱슬 확장에 봤어?

+0

curl 사용자가 얼마나 많은 바이트를 다운 받았는지 사용자에게 알려주고 싶습니다. – Saxtor

+0

나는 컬이 전송 된 바이트를 추적 할 수 있는지 확신합니다. 문서를 읽으십시오. – Kalium

+0

그런 다음 사용자가 파일을 다운로드 할 때 바이트를 추적하는 것이 가능한 방법을 설명 할 수 있습니까? – Saxtor

관련 문제