open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { die('ZIP-Datei konnte nicht erstellt werden.'); } $source = realpath($source); $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); foreach ($files as $file) { $filePath = $file->getRealPath(); $relativePath = substr($filePath, strlen($source) + 1); if ($file->isDir()) { $zip->addEmptyDir($relativePath); } elseif ($file->isFile()) { $zip->addFile($filePath, $relativePath); } } $zip->close(); return true; } // Ausführen if (zipFolder($source, $destination)) { echo "ZIP erfolgreich erstellt: " . basename($destination); } else { echo "Fehler beim Erstellen der ZIP."; }