note 112044 added to ziparchive.addfile
| From: | krisatblacksuitmedia | Date: | Thu, 25 Apr 2013 20:41:42 +0000 |
| Subject: | note 112044 added to ziparchive.addfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194628@lists.php.net to get a copy of this message | ||
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);
//how many file can be added before a reopen is forced?
$filelimit = 245;
// Defines the action
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
// This creates and then gives the option to save the zip file
if ($zip->open($file, ZipArchive::OVERWRITE) !== TRUE) {
die ("Could not open archive");
}
// adds files to the file list
foreach ($filelist as $key=>$value) {
//fix archive paths
$path = str_replace($sourcefolder, "", $key); //remove the source path from the $key to
return only the file-folder structure from the root of the source folder
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);
}
// closes the archive
$zip->close();
//make local temp file a .zip, rename, and move to output dir
rename ($file, "./" . $outputfolder . "/" . $zipfilename);
?>
I hope this may help someone else.
----
Server IP: 69.147.83.199
Probable Submitter: 75.119.73.122
----
Manual Page -- http://php.net/manual/en/ziparchive.addfile.php
Edit -- https://master.php.net/note/edit/112044
Del: integrated -- https://master.php.net/note/delete/112044/integrated
Del: useless -- https://master.php.net/note/delete/112044/useless
Del: bad code -- https://master.php.net/note/delete/112044/bad+code
Del: spam -- https://master.php.net/note/delete/112044/spam
Del: non-english -- https://master.php.net/note/delete/112044/non-english
Del: in docs -- https://master.php.net/note/delete/112044/in+docs
Del: other reasons-- https://master.php.net/note/delete/112044
Reject -- https://master.php.net/note/reject/112044
Search -- https://master.php.net/manage/user-notes.php