2011-07-18 3 views
0

필자는 사람들과 공유하고 개발하기가 더 쉽도록 큰 파일을 가지고 있습니다. 저는 브라우저 기반의 작은 FTP 클라이언트를 개발 중입니다. PHP에서는 standard functions 이상입니다.PHP 파일의 절반 만 다운로드됩니다.

Yesterday I made a question here about how to download AVI files to the client 나는 그렇게 할 this function을 발견

이제
<?php 

/* Tutorial by AwesomePHP.com -> www.AwesomePHP.com */ 
/* Function: download with resume/speed/stream options */ 

/* 
    Parametrs: downloadFile(File Location, File Name, 
    max speed, is streaming   
    If streaming - movies will show as movies, images as images 
    instead of download prompt 
*/ 
     
function downloadFile($fileLocation,$fileName,$maxSpeed = 100,$doStream = 
false){ 
    if (connection_status()!=0) return(false); 
    $extension = strtolower(end(explode('.',$fileName))); 

    /* List of File Types */ 
    $fileTypes['swf'] = 'application/x-shockwave-flash'; 
    $fileTypes['pdf'] = 'application/pdf'; 
    $fileTypes['exe'] = 'application/octet-stream'; 
    $fileTypes['zip'] = 'application/zip'; 
    $fileTypes['doc'] = 'application/msword'; 
    $fileTypes['xls'] = 'application/vnd.ms-excel'; 
    $fileTypes['ppt'] = 'application/vnd.ms-powerpoint'; 
    $fileTypes['gif'] = 'image/gif'; 
    $fileTypes['png'] = 'image/png'; 
    $fileTypes['jpeg'] = 'image/jpg'; 
    $fileTypes['jpg'] = 'image/jpg'; 
    $fileTypes['rar'] = 'application/rar';     
     
    $fileTypes['ra'] = 'audio/x-pn-realaudio'; 
    $fileTypes['ram'] = 'audio/x-pn-realaudio'; 
    $fileTypes['ogg'] = 'audio/x-pn-realaudio'; 
     
    $fileTypes['wav'] = 'video/x-msvideo'; 
    $fileTypes['wmv'] = 'video/x-msvideo'; 
    $fileTypes['avi'] = 'video/x-msvideo'; 
    $fileTypes['asf'] = 'video/x-msvideo'; 
    $fileTypes['divx'] = 'video/x-msvideo'; 

    $fileTypes['mp3'] = 'audio/mpeg'; 
    $fileTypes['mp4'] = 'audio/mpeg'; 
    $fileTypes['mpeg'] = 'video/mpeg'; 
    $fileTypes['mpg'] = 'video/mpeg'; 
    $fileTypes['mpe'] = 'video/mpeg'; 
    $fileTypes['mov'] = 'video/quicktime'; 
    $fileTypes['swf'] = 'video/quicktime'; 
    $fileTypes['3gp'] = 'video/quicktime'; 
    $fileTypes['m4a'] = 'video/quicktime'; 
    $fileTypes['aac'] = 'video/quicktime'; 
    $fileTypes['m3u'] = 'video/quicktime'; 

    $contentType = $fileTypes[$extension]; 
     
     
    header("Cache-Control: public"); 
    header("Content-Transfer-Encoding: binary\n"); 
    header('Content-Type: $contentType'); 

    $contentDisposition = 'attachment'; 
     
    if($doStream == true){ 
        /* extensions to stream */ 
        $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("Accept-Ranges: bytes");    
    $range = 0; 
    $size = filesize($fileLocation); 
     
    if(isset($_SERVER['HTTP_RANGE'])) { 
        list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); 
        str_replace($range, "-", $range); 
        $size2=$size-1; 
        $new_length=$size-$range; 
        header("HTTP/1.1 206 Partial Content"); 
        header("Content-Length: $new_length"); 
        header("Content-Range: bytes $range$size2/$size"); 
    } else { 
        $size2=$size-1; 
        header("Content-Range: bytes 0-$size2/$size"); 
        header("Content-Length: ".$size); 
    } 
         
    if ($size == 0 ) { die('Zero byte file! Aborting download');} 
    set_magic_quotes_runtime(0);  
    $fp=fopen("$fileLocation","rb"); 
     
    fseek($fp,$range); 
   
    while(!feof($fp) and (connection_status()==0)) 
    { 
        set_time_limit(0); 
        print(fread($fp,1024*$maxSpeed)); 
        flush(); 
        ob_flush(); 
        sleep(1); 
    } 
    fclose($fp); 
            
    return((connection_status()==0) and !connection_aborted()); 
}  

/* Implementation */ 
downloadFile('fileLocation','fileName.ext',900,false); 

?> 

내 문제 (약 800 MB) 난 단지 파일의 절반을 얻고있다 (440 MB) 대용량 파일을 다운로드 할 때 내가 가지고있는 것입니다 이것이 왜 있는지 전혀 모르겠습니다. 누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까? 아니면 다른 사람들이 제가 할 수있는 생각이 다른가요?

언급 한 바와 같이, 나는 아래 스크립트를 결합하여 this function을 사용합니다.

require_once 'func_downloadFile.php'; 

    // Get filename 
    $filename = explode("/", $_GET['file']); 
    $filename = $filename[count($filename)-1]; 

    $file_path = "downloads/" . $filename; 

    if(file_exists($file_path)) { 
     downloadFile($file_path, $filename, 900, false); 
    } 
    else { 
     echo "File does not exist."; 
    } 

먼저 파일이 내 NAS에서 내 웹 서버로 다운로드 된 다음 클라이언트로 전송됩니다. 스크립트를 사용하여 클라이언트로 다운로드하는 경우 파일의 절반 만 가져옵니다.하지만 NAS에서 내 웹 서버로 파일을 다운로드 한 다음 tmy 웹 서버와 FTP 클라이언트에 로그인하고 파일을 다운로드하면 전체 파일. (downloadFile()가하는 일입니다) 클라이언트에 웹 서버에서

EDIT를 보낼 때 그래서이 문제는 다음과 같습니다 나는 safe_mode 거짓 반환 if(ini_get('safe_mode')){ 사용에 있었다면 확인했습니다. 따라서 제한 시간을 설정하기 위해 아래 두 줄을 추가하려고 시도했지만 결과는 같습니다.

ini_set('max_execution_time', 0); 
set_time_limit(0); 
+2

스크립트가 시간 초과 될 수 있습니까? – Jacob

+0

@cularis, 가능하다고 들리지만이를 변경하는 방법이 있습니까? 내가 아는 한, ini 파일에서 일부 속성을 변경해야합니다. 맞습니까? – simonbs

답변

0
+0

'safe_mode'가'if (ini_get ('safe_mode')) {{' 거짓으로 돌아왔다. 따라서 원본 게시물에 두 줄을 추가하려고했지만 결과는 같습니다. – simonbs

+0

다운로드가 시작될 때 파일 크기가 무엇인지 알려주므로 서버와 임의의 파일을 사용하여이 작업을 시도하고 있습니다. DL이 시작되면 다운로드가 (이 경우) 350MB라고 알려주고 전체 파일을 다운로드합니다 (아무런 시간 제한 없음). 전송하는 파일의 크기는 얼마나됩니까? – Lukosanthropos

+0

파일은 830MB이며 다운로드하는 동안 올바른 크기를 표시하지만 300-400MB 사이에서 중지됩니다. – simonbs

1

스크립트 실행 제한 시간을 초과 할 가능성이 있습니까? (기본 30초)

당신은 당신은 php.ini 파일을 편집,하지만이를주지 않고 더 높은 한도를 설정할 수 없습니다

while(!feof($fp) and (connection_status()==0)) { 
    set_time_limit(30); 
    ... 
} 

루프에서 시간 제한을 다시 설정하는 함수를 ... 편집 할 수 있습니다 루프는 루프가 반복 될 때마다 루프를 재설정합니다.

httpd 오류 로그에 항목이 있습니까?

+0

'$ doStream'은'false'를 의미하는'if ($ doStream == true) {'에서'set_time_limit'을 사용하면 파일이 스트리밍되지 않습니다. – simonbs

+0

내 실수로, while while (! feof ($ fp) and (connection_status() == 0)) {' –

+0

스크립트에서 ini_set을 사용하여 php.ini에서 설정 한 시간보다 스크립트의 총 시간을 길게 만들 수 있다고 생각하지 않습니다. php.ini에서 한도를 늘리거나'set_time_limit'으로 스크립트를 실행하는 동안 정기적으로 타이머를 재설정 할 수 있습니다. 'set_time_limit'를 호출 할 때마다 타이머를 재설정 할 것입니다. –

1

는 시간 제한 설정을 변경해보십시오 볼 스크립트의 최대 실행 시간을 조정할 수 있습니다. 항목은 max_execution_time이며 php.ini 파일에서 찾을 수 있습니다. 표준은 30 초입니다. (안전 모드에서는 set_time_limit이 작동하지 않습니다.)

+0

'safe_mode'가'if (ini_get ('safe_mode')) {'를 사용하여 켜져 있는지 확인하고 false를 반환했는지 확인하려고했습니다. 따라서 원본 게시물에 두 줄을 추가하려고했지만 결과는 같습니다. – simonbs

관련 문제