2014-07-13 8 views
2

죄송합니다 이것은 새로운 질문이 아니지만 정말이 문제에 갇혀있어. 저는 영화와 같은 큰 파일을 다운로드하기 위해 아래 스크립트를 사용하고 있습니다. 사용자가 파일의 직접 링크에 액세스하도록 할 수 없으며 사용자가 이력서 및 섹션 지원 방식으로 파일을 다운로드 할 수있게해야합니다. 이 코드를 사용하여 섹션이 아닌 지원을 다시 시작합니다. 저는 Yii 프레임 워크를 사용하고 있습니다. 이 문제에 대한 해결책이나 제안을 보내주십시오.이력서 및 PHP에서 섹션 지원

public static function downloadFile($fileLocation, $saveName = null, $maxSpeed = 100, $doStream = false){ 
    $start = 0; 
    $end = -1; 
    $section = false; 
    $extension = CFileHelper::getExtension($fileLocation); 
    $fileName = is_null($saveName) ? basename($fileLocation) : $saveName . '.' . $extension; 
    /* @var $contentType string mime type for the file, if is null, it will be octet-stream */ 
    $contentType = CFileHelper::getMimeType($fileLocation); 
    $contentType = is_null($contentType) ? 'application/octet-stream' : $contentType; 


    if(isset($_SERVER['HTTP_RANGE'])) 
    { 
     $range2 = substr($_SERVER['HTTP_RANGE'], strlen('bytes=')); 

     $range = explode('-', $range2); 

     if($range[0] > 0) 
      $start = intval($range[0]); 
     if($range[1] > 0) 
      $end = intval($range[1]); 

     $section = true; 
    } 

    ob_end_clean(); 
    $old_status = ignore_user_abort(true); 
    set_time_limit(0); 

    $size = filesize($fileLocation); 

    if($start > ($size -1)) $start = 0; 

    $fp = fopen($fileLocation, 'rb'); 
    if($start) fseek($fp, $start); 
    if($end < $start) $end = $size -1; 


    header('Content-Type: '.$contentType); 

    $contentDisposition = 'attachment'; 
    if($doStream == true){ 
     $array_listen = array('mp3','m3u','m4a','mid','ogg','ra','ram','wm', 
     'wav','wma','aac','3gp','avi','mov','mp4','mpeg','mpg','swf','wmv','divx','asf'); 
     if(in_array($extension,$array_listen)){ 
      $contentDisposition = 'inline'; 
     } 
    } 

    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { 
     $fileName= preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1); 
     header("Content-Disposition: $contentDisposition; filename=\"$fileName\""); 
    } else { 
     header("Content-Disposition: $contentDisposition; filename=\"$fileName\""); 
    } 
    header('Content-Disposition: ' . $contentDisposition . '; filename="' . $fileName . '"'); 
    header('Last-Modified: ' . date('D, d M Y H:i:s \G\M\T', filemtime($fileLocation))); 

    if($section) 
    { 
     header("HTTP/1.0 206 Partial Content"); 
     header("Status: 206 Partial Content"); 
     header('Accept-Ranges: bytes'); 
     header("Content-Range: bytes $start-$end/$size"); 
     header("Content-Length: " . ($end - $start + 1)); 

    }else 
     header('Content-Length: '.$size); 

    $size = $end - $start + 1; 

    while(!(connection_aborted() || connection_status() == 1) && !feof($fp)) 
    { 
     print(fread($fp,1024*$maxSpeed)); 
     flush(); 
     ob_flush(); 
     sleep(1); 
    } 
    fclose($fp); 
    ignore_user_abort($old_status); 
    set_time_limit(ini_get('max_execution_time')); 

} 
+0

난 당신이 여기 사람들이 당신을 위해 코드를 작성하지 않을 것을 깨닫게되기를 바랍니다. 게다가 : 같은 문제에 관해서 사이트에 수많은 질문이 있습니다. 최소한 먼저 검색하는 노력을하십시오. – Blizz

+0

나는 @Blizz를 안다. 나는 말했듯이 많은 수색을한다. 나는 내 코드를 쓰는 사람을 찾고 있지 않다. 내가 잘못한 것을 나타내는 단서가 필요하다. – user3817348

답변

0

당신이 아파치를 사용하고 여기에 개조를 설치할 수있는 경우, 당신은 mod_xsendfile 모듈을 사용할 수 있습니다. 그것은 부분 다운로드, 현금 및 기타 멋진 것들을 지원합니다.

간단한 데모 : Send Files Faster & Better with PHP & X-Sendfile