Delete directory before moving

This commit is contained in:
Jay Trees 2022-01-21 14:49:26 +01:00
parent bb248de083
commit f6f2b59311
2 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,24 @@
<?php
/**
* Delete a directory with all of its contents.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
function delete_directory(string $directoryToDelete)
{
foreach (scandir($directoryToDelete) as $filename) {
$filepath = $directoryToDelete . '/' . $filename;
if (is_file($filepath)) {
unlink($filepath);
}
if (is_dir($filepath)) {
delete_directory($filepath);
}
}
unlink($directoryToDelete);
}

View file

@ -110,8 +110,13 @@ if ($zip->open($zip_filename)) {
if (is_file($filepath)) {
echo 'Rename ' . $filepath . ' to ' . __DIR__ . '/' . $filename . '<br>';
rename($filepath, __DIR__ . '/' . $filename);
}
if (is_dir($filepath)) {
delete_directory($filepath);
}
rename($filepath, __DIR__ . '/' . $filename);
}
}