2016-11-04 96 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