tags:

views:

30

answers:

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/");
+1  A: 

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);
madsleejensen
A: 

Bad practice but easy, works on linux only:

<?php
    `rm -rf ./members/9/`;
?>

See the backtick operator and Program execution Functions.

Salman A
will never work on shared hosting, or on any sane production server...
Palantir
@Palantir, of course it can work on shared hosting. Whether hosting is shared or not doesn't affect file system permissions.
Petah
are you serious? do you really think you will find any shared hosting provider who will enable system exec's?? I know that in theory this can be enabled on any server. But no sane system administrator would ever allow that, except on his own internal development server.
Palantir
go to your hosting provider, and check phpinfo(), and check disable_functions. If it contains shell_exec then this will not work.
Palantir