From 87597e7586831e4da38e865cc15ebe0061121e18 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 31 Dec 2015 18:04:33 +0100 Subject: [PATCH] Directory delete function --- System/FileSystem.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/System/FileSystem.php b/System/FileSystem.php index 5ef18d4c7..3e8264261 100644 --- a/System/FileSystem.php +++ b/System/FileSystem.php @@ -15,6 +15,8 @@ */ namespace phpOMS\System; +use phpOMS\Validation\Validator; + /** * Filesystem class. * @@ -87,8 +89,30 @@ class FileSystem { } - public function delete() + public static function deletePath($path) : \bool { + $path = realpath($oldPath = $path); + if ($path === false || !is_dir($path) || Validator::startsWith($path, ROOT_PATH)) { + return false; + } + + $files = scandir($path); + + /* Removing . and .. */ + unset($files[1]); + unset($files[0]); + + foreach ($files as $file) { + if (is_dir($file)) { + self::deletePath($file); + } else { + unlink($file); + } + } + + rmdir($path); + + return true; } public function touch()