php教程

phpreadfile下载大文件失败解决方法

我的站长站 2022-12-29 人阅读

phpreadfile下载大文件失败解决方法,原因是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 "<h1>Content error</h1><p>The file does not exist!</p>";


相关推荐
  • 大文件下载
  • php大文件下载超时中断解决方法

    <?php //设置脚本的最大执行时间,设置为0则无时间限制set_time_limit(0);ini_set(&#39;max_execution_time&#39;, &#39;0&#39;); $file_name = &#39;aa.zip&#39;; //本机文件地址$extension = pathinfo($file_name)[&#39;extension&#39;];$dowloa...

    php教程 39 1年前
  • 大文件分片上传 / 下载限速解决方案

    分片上传将需要上传的文件按照一定的分割规则,分割成相同大小的数据块;初始化一个分片上传任务,返回本次分片上传唯一标识;按照一定的策略(串行或并行)发送各个分片数据块;发送完成后,服务端根据判断数据上传是否完整,如果完整,则进行数据块合成得到原始文件。...

    php教程 40 1年前
  • Nginx + X-Accel大文件下载示例代码

    Nginx + X-Accel大文件下载示例代码,做个记录以后会用到。// 启用 nginx X-Accel 下载 header(&#39;Content-Type: application/octet-stream&#39;); $encoded_fname = &#39;附件名称&#39;; header(&#39;Content-Disposition: a...

    php教程 20 1年前
最新更新