2

Possible Duplicate:
How do I recursively delete a directory and its entire contents (files+sub dirs) in PHP?

How can I delete a directory with other directories and images and php files in it using PHP.

Here is what I'm trying below but with no success.

unlink("./members/9/");
rmdir("./members/9/");
Community
  • 1
  • 1
HELP
  • 14,237
  • 22
  • 66
  • 100

1 Answers1

2

You need to delete all files / folders inside the directory you are trying to delete before you can delete it.

$path = '/path/to/directory';

$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);

for ($dir->rewind(); $dir->valid(); $dir->next()) {
    if ($dir->isDir()) {
        rmdir($dir->getPathname());
    } else {
        unlink($dir->getPathname());
    }
}
rmdir($path);
Mads Lee Jensen
  • 4,570
  • 5
  • 36
  • 53