diff --git a/src/Helper/DirHelper.php b/src/Helper/DirHelper.php index 07343bdd..3406887f 100644 --- a/src/Helper/DirHelper.php +++ b/src/Helper/DirHelper.php @@ -272,4 +272,31 @@ public static function directoryIterator( return new \RecursiveIteratorIterator($filterIterator); } + + /** + * @param string $path + * @return bool + */ + public static function deleteDirectory($path): bool + { + try { + $directoryIterator = new \DirectoryIterator($path); + foreach ($directoryIterator as $fileInfo) { + if ($fileInfo->valid() && $fileInfo->isExecutable()) { + if ($fileInfo->isDot()) { + continue; + } elseif ($fileInfo->isDir()) { + if (self::deleteDirectory($fileInfo->getPathname())) { + rmdir($fileInfo->getPathname()); + } + } elseif ($fileInfo->isFile()) { + unlink($fileInfo->getPathname()); + } + } + } + } catch (\Throwable $e) { + return false; + } + return true; + } }