Reputation: 55
How to delete all the folders inside a parent folder with PHP?
I have tried this, but it isn't working:
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);
}
}
Upvotes: 2
Views: 2468
Reputation: 3676
Try this :
function del($dir)
{
foreach(glob($dir . '/*') as $file)
{
if(is_dir($file))
del($file);
}
rmdir($dir);
}
It will also delete nested folders
Upvotes: 10