note 12993 deleted from function.rmdir by jimw
| From: | jimw@php.net | Date: | Wed, 02 Jan 2002 01:28:28 +0000 |
| Subject: | note 12993 deleted from function.rmdir by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-24092@lists.php.net to get a copy of this message | ||
Here's a recursive directory remover.. removes directories even if they have files inside..
returns 0 on error.. 1 on success... of course you need to have proper permissions to kill all those
directories, but it works well enough for my purposes. You can remove the comments on the echo
statements if you want to see what it's doing as it's doing it.
I also have a recursive directory mover, recursive_mvdir($src,$dest) which moves all files and
directories under $src to a new directory at $dest.. email me if you want it. Not currently a place
for it here as there isn't a directory move function.
add breaks to the echo statements yourself, I tried here but they kept getting interpreted by the
script that posts the note. *shrugs*
function recursive_rmdir($dir)
{
if (!is_dir($dir)) { return 0; }
// echo "\$directory = opendir(\"$dir\");";
$directory = opendir($dir);
// two readdir()'s skip leading . and .. entries
// marginally faster than checking on each
// iteration and besides, you only have two
// and they are at the beginning, why check
// every time?
readdir($directory);
readdir($directory);
while(false !== ($dir_entry = readdir($directory)))
{
if (is_dir($dir."/".$dir_entry))
{
recursive_rmdir($dir."/".$dir_entry);
}
else
{
// echo "unlink(\"$dir/".$dir_entry."\");";
unlink($dir."/".$dir_entry);
}
}
closedir($directory);
unset($directory);
// echo "rmdir(\"$dir\");";
if (rmdir($dir))
{
return 1;
}
else
{
return 0;
}
}
God bless you all.
Paul