note 102340 deleted from function.ziparchive-addfile by danbrown
| From: | danbrown@php.net | Date: | Wed, 09 Feb 2011 17:12:34 +0000 |
| Subject: | note 102340 deleted from function.ziparchive-addfile by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176816@lists.php.net to get a copy of this message | ||
Note Submitter: David Hunt
----
RE: aartdebruijn Note on 1024 file limit when adding files to archive.
Simple solution for creating large archives - break the file into parts during the loop.
/*
(scalar) $zipFile - full path to zip file
(scalar) $filesPath - base path of files list
(array) $filesList - list of files relative to base path
*/
function create_zip( $zipFile, $filesPath, $filesList, $ignoreError=false ){
// Max Timeout
ini_set('max_execution_time', 300);
// Handler
$zip = new ZipArchive();
// Create Archive
if ($zip->open($zipFile, ZIPARCHIVE::CREATE) !== true) {
return 'Failed to create archive';
}
// Append Files to Archive
// Close / Open after Max to overcome server Open File Limits
$loop_inc = 0; $loop_max = 200;
foreach($filesList AS $fn) {
$fp = $filesPath.$fn;
if( file_exists($fp) && is_readable($fp) ){
// Add file to archive
if( $zip->addFile($fp, $fn) !== true )
return 'Failed to add file to archive: '.$fn;
// If Max Reached - Close / Open
if( $loop_inc++ > $loop_max ){
$zip->close();
if( $zip->open( $zipFile ) !== true ){
return 'Failed to open archive for append';
}
$loop_inc = 0;
}
} elseif( !$ignoreError )
return 'Failed to add file to archive: '.$fn;
}
// Close
$zip->close();
return true;
}
/* Example */
$res = create_zip(
'/home/username/www/myfile.zip',
'/home/username/www/images/',
array( 'image1.jpg', 'folder/image2.jpg' )
);
if( $res !== true ) echo 'Whoops! '.$res;