问题描述
我尝试在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文件-达到内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!