From a1a059c74d8afd8070298d4cabdd37d79c586ec3 Mon Sep 17 00:00:00 2001 From: Seth Battis Date: Tue, 13 Feb 2024 19:22:49 -0500 Subject: [PATCH] Filesystem::safeScandir() --- src/Filesystem.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Filesystem.php b/src/Filesystem.php index ea4e1c8..8c0e923 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -22,12 +22,15 @@ private static function recursiveDelete(string $path) if (!is_dir($path)) { return unlink($path); } elseif (is_dir($path)) { - foreach (scandir($path) as $item) { - if ($item !== "." && $item !== "..") { - Filesystem::recursiveDelete(Path::join($path, $item)); - } + foreach (Filesystem::safeScandir($path) as $item) { + Filesystem::recursiveDelete(Path::join($path, $item)); } return rmdir($path); } } + + public static function safeScandir(string $path) + { + return array_values(array_diff(scandir($path), [".", ".."])); + } }