Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<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
You can’t perform that action at this time.