在php中使用readfile下载大文件失败如何解决

今天就跟大家聊聊有关在php中使用readfile下载大文件失败如何解决,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

具体如下:

大文件有200多M,只下载了200K就提示下载完成,且不报错。

原因是PHP内存有限制,需要改为按块下载,就是把大文件切块后逐块下载

if (file_exists($file))
{
  if (FALSE!== ($handler = fopen($file, 'r')))
  {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: chunked'); //changed to chunked
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    //header('Content-Length: ' . filesize($file)); //Remove
    //Send the content in chunks
    while(false !== ($chunk = fread($handler,4096)))
    {
      echo $chunk;
    }
  }
  exit;
}
echo "<h2>Content error</h2><p>The file does not exist!</p>";

看完上述内容,你们对在php中使用readfile下载大文件失败如何解决有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。