2016-11-04 106 views
-3

我使用此代碼從服務器下載/讀取文件。如何在PHP中下載大文件

header("Expires: 0"); 
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache"); 
    header("Content-type: application/file"); 
    header('Content-length: '.filesize($file_to_download)); 
    header('Content-disposition: attachment; filename='.basename($file_to_download)); 
readfile($file_to_download); 
    exit; 

它工作正常,下載文件,但當文件很大時,它顯示錯誤「文件未找到,問題文件加載」。請告訴我在這段代碼中可以更改大文件下載的內容。

回答

0

嘗試這樣的:頁面上

$chunkSize = 1024 * 1024; 
$fd = fopen($file_to_download, 'rb'); 

while (!feof($fd)) { 
    $buffer = fread($fd, $chunkSize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
} 

fclose($fd); 
+0

其節目內容我想顯示像上面我們對它的代碼 –

+0

,但多了一個問題臉部時1個下載開始下載框。我們嘗試下載第二個文件它未打開的顯示頁面處理 –

+0

這工作,我有類似的代碼,但fread處於狀態,我沒有ob_flush。 – jcubic