Created
January 19, 2012 03:12
-
-
Save MilkZoft/1637489 to your computer and use it in GitHub Desktop.
codejobs - Create a Zip file - PHP
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 | |
| function createZip($files = array(), $destination = NULL, $overwrite = FALSE) { | |
| if(file_exists($destination) and !$overwrite) { | |
| return FALSE; | |
| } | |
| $validFiles = array(); | |
| if(is_array($files)) { | |
| foreach($files as $file) { | |
| if(file_exists($file)) { | |
| $validFiles[] = $file; | |
| } | |
| } | |
| } | |
| if(count($validFiles)) { | |
| $Zip = new ZipArchive(); | |
| if(!$Zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)) { | |
| return FALSE; | |
| } | |
| foreach($validFiles as $file) { | |
| $Zip->addFile($file, $file); | |
| } | |
| $Zip->close(); | |
| return file_exists($destination); | |
| } else { | |
| return false; | |
| } | |
| } | |
| function unzipFile($file, $destination) { | |
| $Zip = new ZipArchive(); | |
| if(!$Zip->open($file)) { | |
| die("Could not open archive"); | |
| } | |
| $Zip->extractTo($destination); | |
| $Zip->close(); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment