php readfile下载大文件失败的解决方法

yipeiwu_com6年前PHP代码库

本文实例讲述了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 "<h1>Content error</h1><p>The file does not exist!</p>";

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP网络编程技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP json_encode中文乱码问题的解决办法

下面的PHP代码可以解决以下问题:1.json_encode UTF8码中文后的字符串不可阅读2.json_encode 多级数组中文乱码问题3.json_encode 数组中包含换行时...

基于PHP遍历数组的方法汇总分析

1. foreach()foreach()是一个用来遍历数组中数据的最简单有效的方法。#example1:复制代码 代码如下:<?php$colors= array('red','...

php json_encode奇怪问题说明

json_encode 只支持utf-8格式这个就不多说了 复制代码 代码如下: $array = array ( [0] => array ( [sale_unit_detail...

php数组中删除元素之重新索引的方法

如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 复制代码 代码如下: <?php $arr = array('a','b','c',...

php实现的顺序线性表示例

本文实例讲述了php实现的顺序线性表。分享给大家供大家参考,具体如下: <?php /* * 线性顺序表 ,其是按照顺序在内存进行存储,出起始和结尾以外都是一一连接的...