This problem came up where I needed to remove a folder. Removing a folder is easy, only the PHP command rmdir() only removes the folder if it is empty. The PHP command unlink() is only for files.
So how do I recursively delete a folder that might not be empty? Here is a function for you:
<?
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
<?
rrmdir('folderName');