note 98215 added to function.rmdir
| From: | JohnGalt at osu1 dot php dot net | Date: | Tue, 01 Jun 2010 16:55:42 +0000 |
| Subject: | note 98215 added to function.rmdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169811@lists.php.net to get a copy of this message | ||
Yet another recursive directory deleting function. I made it because it uses less code and
chmod()'s files before attempting to delete them, just in case permissions are denied.
<?php
/***************************************************
* _rmdir(string $path) *
* Deletes the contents of and removes a directory *
* *
* @param string $path Path to the directory *
* *
* Returns bool *
* true if deletion is entirely successful *
* false if deletion is not entirely successful *
***************************************************/
function _rmdir($path)
{
$path = ((substr($path, -1) != PATH_SEPARATOR) ? ($path.PATH_SEPARATOR) : ($path));
if(($open = opendir($path)) === false)
return false;
while(($fn = readdir($open)) !== false)
if($fn != "." && $fn != "..")
{
@chmod($path.$fn, 0777);
if(((is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) &&
!_rmdir($path.$fn))))
return false;
}
closedir($open);
return rmdir($path);
}
?>
As you can see, it also checks if the directory exists before deleting, and appends a slash
separator to the path if necessary. And it doesn't use glob() because I'm running off an
old PHP server that doesn't support it. I don't know which takes more server resources,
glob() or opendir()/readdir()/closedir().
----
Server IP: 69.147.83.197
Probable Submitter: 72.208.240.199
----
Manual Page -- http://www.php.net/manual/en/function.rmdir.php
Edit -- https://master.php.net/note/edit/98215
Del: integrated -- https://master.php.net/note/delete/98215/integrated
Del: useless -- https://master.php.net/note/delete/98215/useless
Del: bad code -- https://master.php.net/note/delete/98215/bad+code
Del: spam -- https://master.php.net/note/delete/98215/spam
Del: non-english -- https://master.php.net/note/delete/98215/non-english
Del: in docs -- https://master.php.net/note/delete/98215/in+docs
Del: other reasons-- https://master.php.net/note/delete/98215
Reject -- https://master.php.net/note/reject/98215
Search -- https://master.php.net/manage/user-notes.php