How can I delete a directory with other directories and images and php files in it using PHP – Education Career Blog

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/");

,

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);

Leave a Comment