Created
January 24, 2013 17:41
-
-
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)
This file contains hidden or 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
<?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