Php rendering of a large zip file - memory limit reached

I am trying to make a zip file in php. The code:

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

The downloaded file contains only a few bytes. This error message is:

<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 want to increase memory_limit in php.ini. What are the alternative ways to correctly display large zip files without using global settings?

+1
source share
2 answers

The download stream, so it does not suffocate in memory. A tiny example:

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

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

+4
source

PHP Apache readfile():

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

All Articles