open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { echo 'Failed to create ZIP file.'; exit; } function addDirToZip($zip, $dir, $rootPath) { $files = scandir($dir); foreach ($files as $file) { if ($file != '.' && $file != '..') { $path = $dir . '/' . $file; $relPath = substr($path, strlen($rootPath) + 1); if (is_dir($path)) { $zip->addEmptyDir($relPath); addDirToZip($zip, $path, $rootPath); } else { $zip->addFile($path, $relPath); } } } } addDirToZip($zip, $projectDir, $projectDir); $zip->close(); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . $project . '.zip"'); header('Content-Length: ' . filesize($zipFile)); readfile($zipFile); unlink($zipFile); exit; ?>