function folderToZip($folder, &$zipFile, $subfolder = 'deploy') { if ($zipFile == null) { // no resource given, exit return false; } // we check if $folder has a slash at its end, if not, we append one $folder .= end(str_split($folder)) == "/" ? "" : "/"; $subfolder .= end(str_split($subfolder)) == "/" ? "" : "/"; // we start by going through all files in $folder $handle = opendir($folder); while ($f = readdir($handle)) { if ($f != "." && $f != "..") { if (is_file($folder . $f)) { // if we find a file, store it // if we have a subfolder, store it there //if ($subfolder != null) $zipFile->addFile($folder . $f, $subfolder . $f); //else // $zipFile->addFile($folder . $f); } elseif (is_dir($folder . $f)) { // if we find a folder, create a folder in the zip // if($subfolder==null) // { // $zipFile->addEmptyDir($f); // and call the function again // folderToZip($folder . $f, $zipFile, $f); // } // else //{ $zipFile->addEmptyDir($subfolder . $f); // and call the function again folderToZip($folder . $f, $zipFile, $subfolder . $f); //} } } } }
function folderToZip($folder, &$zipFile, $exclusiveLength, $encoding, $imgDateNewer, $withfolder, $debug = 0) { $handle = opendir($folder); while (false !== ($f = readdir($handle))) { if ($f != '.' && $f != '..' && $f != '') { $filePath = realpath("{$folder}/{$f}"); // Remove prefix from file path before add to zip. $localPath = substr($filePath, $exclusiveLength); if ($localPath[0] == "/") { $localPath = substr($localPath, 1); } if (is_file($filePath)) { $lastchange = filemtime($filePath); if ($lastchange == null or $lastchange >= $imgDateNewer) { $localPath = convert_string($localPath, null, $encoding); $filenamezip = basename($localPath); //if($withfolder) $filenamezip="$localPath\\".$filenamezip; if ($withfolder) { $filenamezip = "{$localPath}"; } $zipFile->addFile($filePath, $filenamezip); //$zipFile->addFile($filePath, $localPath); } } elseif (is_dir($filePath)) { // Add sub-directory. //$localPath=convert_string($localPath, null , $encoding); //$zipFile->addEmptyDir($localPath); folderToZip($filePath, $zipFile, $exclusiveLength, $encoding, $imgDateNewer, $withfolder); } if ($debug >= 2) { echo $localPath . "<br>"; } //*/ } } closedir($handle); }
if ($zipFile == null) { // no resource given, exit return false; } // we check if $folder has a slash at its end, if not, we append one $folder .= end(str_split($folder)) == "/" ? "" : "/"; // we start by going through all files in $folder $handle = opendir($folder); while ($f = readdir($handle)) { if ($f != "." && $f != "..") { if (is_file($folder . $f)) { // if we find a file, store it $zipFile->addFile($folder . $f); } } } } $z = new ZipArchive(); $z->open($zip_file, ZIPARCHIVE::CREATE); folderToZip($source_dir, $z); $z->close(); ?> <html> <body> <a href="<?php echo $zip_file; ?> " style="font-size: 30px; font-weight: bold;">Download file</a> </body> </html>
echo "Kann Klassenverzeichnis {$dir} nicht finden"; exit(0); } //Dateinamen für Temp-Zip-Datei $filename = session_id() . ".zip"; $filepath = "../" . $_SESSION["settings"]["temp_image_file_path"]; $filename_zip = $filepath . "/" . $filename; if (file_exists($filename_zip)) { unlink($filename_zip); } $zip = new ZipArchive(); if ($zip->open($filename_zip, ZIPARCHIVE::CREATE) !== TRUE) { exit("cannot open <{$filename_zip}>\n"); } //Verzeichnis hinzufügen folderToZip($dir, $zip, $exclusiveLength, $zipencoding); $zip->close(); $size = filesize($filename_zip); if ($size <= 1) { echo "Konnte Zip-Datei nicht erstellen!"; } else { // http headers for zip downloads header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"" . $class . ".zip\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($filepath . $filename));