So this code works fine for zipping up one directory, but the problem is that I have a directory full of directories, and i want to selectively pick out two of these directories and create a zip file out of them...
so i have:
/directory/folder1
/directory/folder2
/directory/folder3
/directory/folder4
/directory/bonusfolder
i want the zip file to create a zip that includes for example, folder1 + bonusfolder in the same zip..
the function below zips a single directory recursively, but i need it to be able to zip 2 directories recursively... any advise would be great 🙂
<?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));
}
return $zip->close();
}
?>