1

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

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

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

<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 文件?

4

2 回答 2

4

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.

于 2011-06-08T17:49:37.583 回答
0

PHP 实际上提供了一种简单的方法,可以将二进制文件直接输出到 Apache,而无需先通过readfile()函数将其存储在内存中:

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile('file.zip');
于 2014-08-04T03:33:18.563 回答