荟萃馆

位置:首页 > 计算机 > php语言

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

php语言1.26W

项目遇见了一个错误。下载报表时数据量太大,文件达到了300多M,这时出现了“页面未找到”的错误,开始以为是服务器文件不存在,后来看了下确实有的,然后尝试了多次,发现小文件是可以下载的。于是确定是文件太大的原因。以下是小编为大家搜索整理的'php readfile下载大文件失败的解决方法,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!

ALt="php readfile下载大文件失败的解决方法" title="php readfile下载大文件失败的解决方法">

  具体如下:

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

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

?

123456789101112131415161718192021if (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 chunkedheader('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 chunkswhile(false !== ($chunk = fread($handler,4096))){echo $chunk;}}exit;}echo " Content error

The file does not exist!

";

PHP实现远程下载文件到本地

PHP文件下载类

php做下载文件的实现代码及文件名中乱码解决方法

php使浏览器直接下载pdf文件的方法

php实现的支持断点续传的文件下载类

php中强制下载文件的代码(解决了IE下中文文件名乱码问题)b

标签:PHP readfile 文件