Created
December 17, 2011 19:36
-
-
Save ryanbattles/1491163 to your computer and use it in GitHub Desktop.
Download site as .zip
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
<!--http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php--> | |
<?php | |
function Zip($source, $destination) | |
{ | |
if (!extension_loaded('zip') || !file_exists($source)) { | |
return false; | |
} | |
$zip = new ZipArchive(); | |
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { | |
return false; | |
} | |
$source = str_replace('\\', '/', realpath($source)); | |
if (is_dir($source) === true) | |
{ | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) | |
{ | |
$file = str_replace('\\', '/', realpath($file)); | |
if (is_dir($file) === true) | |
{ | |
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
} | |
else if (is_file($file) === true) | |
{ | |
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); | |
} | |
} | |
} | |
else if (is_file($source) === true) | |
{ | |
$zip->addFromString(basename($source), file_get_contents($source)); | |
} | |
printf('done'); | |
return $zip->close(); | |
} | |
Zip('./directory/', './compressed.zip'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment