2013-08-06 67 views
0

以下腳本是我用來強制下載的。ForceDownload中的文件大小問題

// grab the requested file's name 
$file_name = $_GET['file']; 

// make sure it's a file before doing anything! 
if(is_file($file_name)) { 

    /* 
     Do any processing you'd like here: 
     1. Increment a counter 
     2. Do something with the DB 
     3. Check user permissions 
     4. Anything you want! 
    */ 

    // required for IE 
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } 

    // get the file mime type using the file extension 
    switch(strtolower(substr(strrchr($file_name, '.'), 1))) { 
     case 'pdf': $mime = 'application/pdf'; break; 
     case 'zip': $mime = 'application/zip'; break; 
     case 'jpeg': 
     case 'jpg': $mime = 'image/jpg'; break; 
     default: $mime = 'application/force-download'; 
    } 
    header('Pragma: public'); // required 
    header('Expires: 0');  // no cache 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT'); 
    header('Cache-Control: private',false); 
    header('Content-Type: '.$mime); 
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Content-Length: '.filesize($file_name)); // provide file size 
    header('Connection: close'); 
    readfile($file_name);  // push it out 
    exit(); 

} 

的問題是,上面的代碼正常工作時間少於100MB文件,它不能工作,例如用於文件了超過200MB,並說下載177個字節。

我該如何擺脫這個問題?

編輯1:

主要腳本是從here複製。

謝謝!

+3

有一條線關於最大上傳大小的php.ini文件。將其改爲任何你想要的。這應該可以解決你的問題。 'upload_max_filesize = 20M'例如 –

+1

除了之前的評論:請確保您的post_max_size也正確對齊。 –

+0

我不明白上面的代碼與上傳有什麼關係? – BenLanc

回答

2

我懷疑你是通過一次性將文件加載到內存中導致PHP使用太多內存 - 查看下載文件的內容,並且您可能會看到它是純文本幷包含PHP致命的錯誤消息。

你會很好地加載較小的塊文件,並將其傳遞迴Web服務器服務,例如,嘗試換掉「ReadFile的」符合以下幾點:

// Open the file for reading and in binary mode 
$handle = fopen($file_name,'rb'); 
$buffer = ''; 

// Read 1MB of data at a time, passing it to the output buffer and flushing after each 1MB 
while(!feof($handle)) 
{ 
    $buffer = fread($handle, 1048576); 
    echo $buffer; 
    @ob_flush(); 
    @flush(); 
} 
fclose($handle);