如何在PHP中下载大文件

如何在PHP中下载大文件,php,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:

我使用此代码从服务器下载/读取文件

  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;
下载文件工作正常,但当文件较大时,会显示错误“未找到文件,文件加载有问题”。请告诉我,对于大文件下载,我可以对这段代码进行哪些更改。

尝试如下:

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

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

fclose($fd);

它在页面上的显示内容我想要显示下载框,就像上面我们的代码一样。这很有效,我有过类似的代码,但fread处于while状态,我没有ob_flush。