php渲染大型zip文件-达到内存限制 [英] php rendering large zip file - memory limit reached

查看:239
本文介绍了php渲染大型zip文件-达到内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在php中呈现一个zip文件. 代码:

I try to render a zip file in php. Code:

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');

下载的文件只有几个字节.这是一条错误消息:

The downloaded file, is only few bytes. It is an error message:

<br /> <b>Fatal error</b>: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41908867 bytes) in <b>/var/www/common_index/main.php</b> on line <b>217</b><br />

<br /> <b>Fatal error</b>: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41908867 bytes) in <b>/var/www/common_index/main.php</b> on line <b>217</b><br />

我不希望增加php.ini中的memory_limit.有什么其他方法可以正确渲染大型zip文件而无需修改全局设置?

I do not wish to increase memory_limit in php.ini. What are alternative ways to properly render large zip files without tinkering with global settings?

推荐答案

流式传输下载内容,因此不会占用内存. 小例子:

Stream the download, so it doesn't choke on memory. Tiny example:

$handle = fopen("exampe.zip", "rb");
while (!feof($handle)) {
    echo fread($handle, 1024);
    flush();
}
fclose($handle);

添加正确的输出标题以进行下载,您应该解决该问题.

Add correct output headers for downloading, and you should solve the problem.

这篇关于php渲染大型zip文件-达到内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆