Skip to content

Commit

Permalink
Update zip-folder.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ttodua authored Aug 21, 2023
1 parent 8c9b382 commit b9371f2
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions zip-folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@
// =========================================================================

function zip_folder ($input_folder, $output_zip_file) {
$zipClass = new ZipArchive();
if($input_folder !== false && $output_zip_file !== false)
{
$res = $zipClass->open($output_zip_file, \ZipArchive::CREATE);
if($res === TRUE) {
// Add a Dir with Files and Subdirs to the archive
$foldername = basename($input_folder);
$zipClass->addEmptyDir($foldername);
$foldername .= '/'; $input_folder .= '/';
// Read all Files in Dir
$dir = opendir ($input_folder);
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') continue;
// Rekursiv, If dir: GoodZipArchive::addDir(), else ::File();
$do = (filetype( $input_folder . $file) == 'dir') ? 'addDir' : 'addFile';
$zipClass->$do($input_folder . $file, $foldername . $file);
}
$zipClass->close();
}
else { exit ('Could not create a zip archive, migth be write permissions or other reason. Contact admin.'); }
}
}
$zipClass = new ZipArchive();
$input_folder = realpath($input_folder);
$addDirDo = static function($input_folder, $name) use ($zipClass, &$addDirDo ) {
$name .= '/';
$input_folder .= '/';
// Read all Files in Dir
$dir = opendir ($input_folder);
while ($item = readdir($dir)) {
if ($item == '.' || $item == '..') continue;
$itemPath = $input_folder . $item;
if (filetype($itemPath) == 'dir') {
$zipClass->addEmptyDir($name . $item);
$addDirDo($input_folder . $item, $name . $item);
} else {
$zipClass->addFile($itemPath, $name . $item);
}
}
};
if($input_folder !== false && $output_zip_file !== false)
{
$res = $zipClass->open($output_zip_file, \ZipArchive::CREATE);
if($res === true) {
$zipClass->addEmptyDir(basename($input_folder));
$addDirDo($input_folder, basename($input_folder));
$zipClass->close();
}
else { exit ('Could not create a zip archive, migth be write permissions or other reason.'); }
}
}

0 comments on commit b9371f2

Please sign in to comment.