Question

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);
  }
}
Was it helpful?

Solution

Try this :

function del($dir) 
{ 
  foreach(glob($dir . '/*') as $file) 
  { 
    if(is_dir($file))
        del($file); 
  } 
  rmdir($dir); 
}

It will also delete nested folders

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top