2013-10-14 68 views
0

嘿,我有下面的代碼來下載一個大文件,但下載確實停止,每次不完成下載下載不起作用

function download($file) 
{ 
    include('logger.php5'); 
    $log = new Logging(); 
    $log->lfile('download.log'); 
    ini_set('max_execution_time', 86400); 
    //header('Location: '.$file); 
    $filesize = filesize($file); 
    $filename = pathinfo($file, PATHINFO_BASENAME); 
    $filext = pathinfo($file, PATHINFO_EXTENSION); 
    $mime = include('mime.php5'); 

    $log->lwrite(ini_get('max_execution_time')); 
    $log->lwrite(sprintf('%s %s %s %s', $filename, $filext, $mime[$filext], human_filesize($filesize))); 
    $log->lclose(); 
    @ob_end_clean(); 
    session_write_close(); 
    header("Content-Description: File Transfer"); 
    header("Content-Type: ".$mime[$filext]); 
    header("Content-Disposition: ". 
    (!strpos($HTTP_USER_AGENT,"MSIE 5.5")?"attachment; ":""). 
    "filename=".$filename); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".$filesize); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: public"); 
    header('Pragma: public'); 
    header('Expires: 0'); 
    $done = readfile_chunked($file); 
} 

function readfile_chunked($filename,$retbytes=true) { 
    $chunksize = 1*(1024*1024); // how many bytes per chunk 
    $buffer = ''; 
    $cnt =0; 
    // $handle = fopen($filename, 'rb'); 
    $handle = fopen($filename, 'rb'); 
    if ($handle === false) { 
     return false; 
    } 
    while (!feof($handle)) { 
     $buffer = fread($handle, $chunksize); 
     echo $buffer; 
     ob_flush(); 
     flush(); 
     if ($retbytes) { 
      $cnt += strlen($buffer); 
     } 
    } 
     $status = fclose($handle); 
    if ($retbytes && $status) { 
     return $cnt; // return num. bytes delivered like readfile() does. 
    } 
    return $status; 
} 

我每次調用腳本下載啓動,但後停止400MB,文件本身大778MB。

有人可以看到代碼有問題嗎?

UPDATE

嘗試後登錄的readfile_chunked返回值的感覺就像劇本被採空不下載本身。因爲我不能在readfile_chunked調用後獲得日誌條目。

+0

所有頭文件被設置在'下載'功能外面只是安全的東西來識別正確的文件 –

+0

是啊正確的頭文件被髮送。 –

+0

當你說下載停止,你的意思是客戶端只收到400MB?或者你的意思是'readfile_chunked'函數返回一個400MB的計數?或兩者? –

回答

-1

這可能是PHP中的filesize函數的問題。有對大文件大小的閱讀已知錯誤,當​​你正在與文件作爲首部發送它,我建議你試試這個腳本沒有使用這條線:

header("Content-Length: ".$filesize); 

哦,也許你可以去看看在這一行:

header("Content-Transfer-Encoding: binary"); 

我認爲應該檢查每個文件的編碼。像這樣:

$finfo = finfo_open(FILEINFO_MIME); 

//check to see if the mime-type starts with 'text' 
return substr(finfo_file($finfo, $filename), 0, 4) == 'text'; 

如果是文本文件,你應該使用ASCII of course。與這個問題無關,但我認爲這是一個有用的除了你的腳本:)