Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Created February 20, 2011 13:39
Show Gist options
  • Save MasterEx/835970 to your computer and use it in GitHub Desktop.
Save MasterEx/835970 to your computer and use it in GitHub Desktop.
PHP zip file creation
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Periklis Ntanasis
* ----------------------------------------------------------------------------
*/
function zipFiles($destination,$files=array(),$file_names=array(),$overwrite=true) {
if(file_exists($destination) && !$overwrite)
return false;
$zip = new ZipArchive();
$res = $zip->open($destination,$overwrite ? ZipArchive::OVERWRITE : ZipArchive::CREATE);
if(!$res)
return false;
if(count($file_names)>0)
$with_names=true;
else
$with_names=false;
for($i=0;$i<count($files);$i++)
{
if($with_names)
$zip->addFile($files[$i],$file_names[$i]);
else
$zip->addFile($files[$i]);
}
$zip->close();
return file_exists($destination);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment