Created
January 14, 2011 07:35
-
-
Save gWorldz/779319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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