1) If you want to add files to a ZIP archive but you don't know if the ZiP file exists or not, you MUST check: this changes the way you open it !.
2) you can not append multiple flags, can use only one (or none).
If the zip does not exists, open it with:
$ziph->open($archiveFile, ZIPARCHIVE::CM_PKWARE_IMPLODE)
(or a different compression method)
If the zip already exists, open it with:
$ziph->open($archiveFile)
or
$ziph->open($archiveFile, ZIPARCHIVE::CHECKCONS)
Example: make backup files every hour and ZIP them all in a daily ZIP archive, so you want to get only one ZIP per day, each ZIP containing 24 files:
<?php
function archivebackup($archiveFile, $file, &$errMsg)
{
$ziph = new ZipArchive();
if(file_exists($archiveFile))
{
if($ziph->open($archiveFile, ZIPARCHIVE::CHECKCONS) !== TRUE)
{
$errMsg = "Unable to Open $archiveFile";
return 1;
}
}
else
{
if($ziph->open($archiveFile, ZIPARCHIVE::CM_PKWARE_IMPLODE) !== TRUE)
{
$errMsg = "Could not Create $archiveFile";
return 1;
}
}
if(!$ziph->addFile($file))
{
$errMsg = "error archiving $file in $archiveFile";
return 2;
}
$ziph->close();
return 0;
}
?>