Skip to content

Instantly share code, notes, and snippets.

@gWorldz
Created January 14, 2011 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gWorldz/779319 to your computer and use it in GitHub Desktop.
Save gWorldz/779319 to your computer and use it in GitHub Desktop.
<pre><?php
function ZipFolder($folder, $to='archive.zip', $basedir) {
$zip = new ZipArchive();
if ($zip->open($to, ZIPARCHIVE::CREATE)) {
$found = array(rtrim($folder,DIRECTORY_SEPARATOR.'\/'));
while ($path = each($found)) {
$path = current($path);
if (is_dir($path)) {
$zip->addEmptyDir(substr($path, strlen($basedir)));
foreach (scandir($path) as $subpath) {
if ($subpath=='.'||$subpath=='..'||substr($subpath,-2)==DIRECTORY_SEPARATOR.'.'||substr($subpath,-3)==DIRECTORY_SEPARATOR.'..') continue;
$found[] = $path.DIRECTORY_SEPARATOR.$subpath;
}
} else {
$zip->addFile($path, substr($path, strlen($basedir)));
}
}
if ($zip->close()) {
header ("Content-Type: application/zip");
header ("Content-Disposition: attachment; filename=$to");
header ("Pragma: no-cache");
header ("Expires: 0");
if (!readfile($to)){
print 'Error, there was a problem creating the zip file for the template directoty.';
}
if (!unlink($to)) {
print 'Error, there was a problem deleting the zip file of the template directoty.';
}
return true;
} else {
print 'Error, could not finalise the archive.';
}
} else {
print 'Error, could not create a zipfile at '.$to;
}
return false;
}
ZipFolder('logs');
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment