Created
September 2, 2019 04:30
-
-
Save frumbert/10dc89c918e5ea20f18a58bff1f147fd to your computer and use it in GitHub Desktop.
zip each folder in current directory recursively without the top-level folder, and name it after the directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("content-type:text/plain"); | |
$root = realpath("."); | |
$ar = array_diff(scandir($root), array('.','..')); | |
foreach ($ar as $fold) { | |
$rootPath = "{$root}/{$fold}"; | |
if (is_dir($rootPath)) { | |
$zip = new ZipArchive(); | |
$zip->open("{$rootPath}.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE); | |
$files = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY | |
); | |
foreach ($files as $name => $file) { | |
if (!$file->isDir()) { | |
$filePath = $file->getRealPath(); | |
$relativePath = substr($filePath, strlen($rootPath) + 1); | |
$zip->addFile($filePath, $relativePath); | |
} | |
} | |
$zip->close(); | |
echo "{$fold}.zip", PHP_EOL; | |
} | |
} | |
echo "Done."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment