Skip to content

Instantly share code, notes, and snippets.

@jonashansen229
Created January 24, 2013 17:41
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 jonashansen229/4625593 to your computer and use it in GitHub Desktop.
Save jonashansen229/4625593 to your computer and use it in GitHub Desktop.
Function that zip's a directory. Including all files and sub-directories. @param string dirname(name of target dir), @param object zip (object of ZipArchive)
<?php
public function zipFiles($dirName, $zip) {
$zipName = $dirName.'.zip';
$filenames = array();
$path = $this->_uploadFolder.$dirName.'/';
ini_set('max_execution_time', 5000);
// Opening and creating zip file
if($zip->open($path.$zipName, $overwrite ? ZIPARCHIVE::OVERWRITE :
ZIPARCHIVE::CREATE) !== true) {
return "Failed to open directory to zip!";
}
// initialize an iterator and tpass in the target directory
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
// Iterates over a directory
foreach ($iterator as $key => $value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file $key");
}
// close and save the archive
$zip->close();
return $zipName;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment