note 90668 modified in function.rmdir by danbrown

From: Date: Mon, 04 May 2009 12:55:32 +0000
Subject: note 90668 modified in function.rmdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154181@lists.php.net to get a copy of this message
There should be added a new parameter which allows to specify that I want to delete a folder with all files in it. The following function does not delete subdirectories but all files which is often enough. <?php public static function RemoveDirectory($Dir) { if (!is_dir($Dir)) return true; $Files = scandir($Dir); foreach ($Files as $File) { if ($File == "." || $File == "..") continue; if (!Utils::DeleteFile($Dir.$File)) return false; } if (rmdir($Dir)) return true; Log::Error("Could not delete directory: '$Dir'"); return false; } public static function DeleteFile($File) { if (!is_file($File)) return true; if (unlink($File)) return true; Log::Error("Could not delete file: '$File'"); return false; } ?> --was-- There should be added a new parameter which allows to specify that I want to delete a folder with all files in it. The following function does not delete subdirectories but all files which is often enough. public static function RemoveDirectory($Dir) { if (!is_dir($Dir)) return true; $Files = scandir($Dir); foreach ($Files as $File) { if ($File == "." || $File == "..") continue; if (!Utils::DeleteFile($Dir.$File)) return false; } if (rmdir($Dir)) return true; Log::Error("Could not delete directory: '$Dir'"); return false; } public static function DeleteFile($File) { if (!is_file($File)) return true; if (unlink($File)) return true; Log::Error("Could not delete file: '$File'"); return false; } http://php.net/manual/en/function.rmdir.php

« previous php.notes (#154181) next »