Here is the simple, easy to read, recursive function that works very well:
function zip_r($from, $zip, $base=false) {
if (!file_exists($from) OR !extension_loaded('zip')) {return false;}
if (!$base) {$base = $from;}
$base = trim($base, '/');
$zip->addEmptyDir($base);
$dir = opendir($from);
while (false !== ($file = readdir($dir))) {
if ($file == '.' OR $file == '..') {continue;}
if (is_dir($from . '/' . $file)) {
zip_r($from . '/' . $file, $zip, $base . '/' . $file);
} else {
$zip->addFile($from . '/' . $file, $base . '/' . $file);
}
}
return $zip;
}
$from = "/path/to/folder";
$base = "basezipfolder";
$zip = new ZipArchive();
$zip->open('zipfile.zip', ZIPARCHIVE::CREATE);
$zip = zip_r($from, $zip, $base);
$zip->close();