I had a huge number of files and folders that I needed to zip on a linux web server. I was running into timeout problems and file enumerator issues, as well as file handler limit issues (ulimit). I used a script to solve u limit offered by Farzad Ghanei first (ZipArchiveImproved), but closing and reopening his way didn't do the trick for me.
I eventually did a simple call to a $filelimit variable I created that records file handler limit I want my script to hit before it closes and reopens the file.
<?php
$filelimit = 255;
if ($zip->numFiles == $filelimit) {$zip->close(); $zip->open($file) or die ("Error: Could not reopen Zip");}
?>
This made some progress for me, timeouts were gone, but when calling
<?php $zip->addFile($filepath, $archivefilepath); ?>
after the reopening of the Zip, I got an error. I echoed the <?php $zip->numFiles; ?> and found that after reopening, the numFile enum reset to '0'.
A few more goose-chases later, I tried addFromString with some better results, but did not get it working 100% until I actually coupled addFromString with addFile! My working scripting for the add files function on massive file-folder structures looks like so:
<?php
$sourcefolder = /rel/path/to/source/folder/on/server/
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);
$filelimit = 245;
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
if ($zip->open($file, ZipArchive::OVERWRITE) !== TRUE) {
die ("Could not open archive");
}
foreach ($filelist as $key=>$value) {
$path = str_replace($sourcefolder, "", $key); if (!file_exists($key)) { die($key.' does not exist. Please contact your administrator or try again later.'); }
if (!is_readable($key)) { die($key.' not readable. Please contact your administrator or try again later.'); }
if ($zip->numFiles == $filelimit) {$zip->close(); $zip->open($file) or die ("Error: Could not reopen Zip");}
$zip->addFromString($path, $key) or die ("ERROR: Could not add file: $key </br> numFile:".$zip->numFiles);
$zip->addFile(realpath($key), $path) or die ("ERROR: Could not add file: $key </br> numFile:".$zip->numFiles);
}
$zip->close();
rename ($file, "./" . $outputfolder . "/" . $zipfilename);
?>
I hope this may help someone else.