Code: Select all
echo"<p>Zip my Files</p>";
$files=array('fileA.txt','fileB.txt','fileC.txt');
if(create_zip($files, 'MYZIP.zip'))
echo "Files Zipped Successfully";
function create_zip($files = array(),$destination = '',$overwrite = false)
{
if(file_exists($destination) && !$overwrite)
{
echo "Destination already exists";
return false;
}
$valid_files = array();
if(is_array($files))
{
foreach($files as $file)
{
if(file_exists($file))
{
$valid_files[] = $file;
}
}
}
if(count($valid_files))
{
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)
{
echo "Can't zip files";
return false;
}
foreach($valid_files as $file)
{
$zip->addFile($file,$file);
}
echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status ,'<br>';
$zip->close();
return file_exists($destination);
}
else
{
echo "No file found to zip";
return false;
}
}