views:

618

answers:

3

I am using PHP to move the contents of a images subfolder

GalleryName/images/

into another folder. After the move, I need to delete the GalleryName directory and everything else inside it.

I know that rmdir() won't work unless the directory is empty. I've spent a while trying to build a recursive function to scandir() starting from the top and then unlink() if it's a file and scandir() if it's a directory, then rmdir() each empty directory as I go.

So far it's not working exactly right, and I began to think -- isn't this a ridiculously simple function that PHP should be able to do? Removing a directory?

So is there something I'm missing? Or is there at least a proven function that people use for this action?

Any help would be appreciated.

PS I trust you all here more than the comments on the php.net site -- there are hundreds of functions there but I am interested to hear if any of you here recommend one over others.

A: 

Here is a good implementation:

Remove Directories Recursively with PHP

At many times we need to empty a directory. Of course PHP has the filesystem functions like unlink() and rmdir() to delete files and directories. At first we may think that simply use rmdir() will solve the problem. Unfortunately it's not. rmdir() only removes empty directory. If the directory is not empty, it will return false.

In order to remove a directory and its contents, we have to remove its contents first, consisting of files and subdirectories. To make things more complicated, the subdirectories also contains files and another subdirectories in it.

It seems like this is a difficult task to solve. But in contrast, this is a very simple recursive function.

Andrew Hare
Yes, I know all this. Thanks.
Jason Rhodes
Well, you aren't going to find a much better implementation and none exists in the PHP lib so I would recommend that you use this :)
Andrew Hare
The link is broken
rogeriopvl
Andrew, I'm sorry. I didn't see your link and was only replying to your text below the link. And yes, the link is broken. Thanks anyway.
Jason Rhodes
+1  A: 

This is another good implementation:

http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

inakiabt
This is great, thanks. Great commenting.
Jason Rhodes
Link is broken. Maybe you could copy the solution here.
grom
Seems it's back :)
inakiabt
A: 

This is the recursive function I've created/modifed and that finally seems to be working. Hopefully there isn't anything too dangerous in it. (Note: I've defined DS to the DIRECTORY_SEPARATOR in an init.php file.

function destroy_dir($dir) { 
    if (!is_dir($dir) || is_link($dir)) return unlink($dir); 
        foreach (scandir($dir) as $file) { 
            if ($file == '.' || $item == '..') continue; 
            if (!destroy_dir($dir.DS.$file)) { 
                chmod($dir.DS.$file, 0777); 
                if (!destroy_dir($dir.DS.$file)) return false; 
            }; 
        } 
        return rmdir($dir); 
    }
Jason Rhodes