Possible Duplicate:
How do I recursively delete a directory and its entire contents (files+sub dirs) in PHP?
I need to delete a directory in PHP. There's no built-in way to remove directories with files in them, so I know I'll have to use someone else's script.
I've found, and I think I'll use:
<?php
// simply pass the directory to be deleted to this function
function rrmdir($dir)
{
if (is_dir($dir)) // ensures that we actually have a directory
{
$objects = scandir($dir); // gets all files and folders inside
foreach ($objects as $object)
{
if ($object != '.' && $object != '..')
{
if (filetype($dir . '/' . $object) == 'dir')
{
// if we find a directory, do a recursive call
rrmdir($dir . '/' . $object);
}
else
{
// if we find a file, simply delete it
unlink($dir . '/' . $object);
}
}
}
// the original directory is now empty, so delete it
rmdir($dir);
}
}
?>
I can't see anything wrong with it, I'm just a bit nervous about not only deleting files, but entire directories, and recursively at that.
Is there any reason this wouldn't work? Have you used anything different?