Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

php rendering large zip file - memory limit reached

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 />

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...