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;
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.