note 103140 deleted from function.rmdir by danbrown
| From: | danbrown@php.net | Date: | Mon, 28 Mar 2011 21:01:34 +0000 |
| Subject: | note 103140 deleted from function.rmdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178279@lists.php.net to get a copy of this message | ||
Note Submitter: jonas dot brekle at gmail dot com
----
i extended the last proposal by two (for my understanding) important features:
- atomicity
if the operation fails somewhere in the recursive tree, we have a half-deleted folder. this is not
nice. i added a recursive check wether the folder (and everything in it) is writeabel. the deletion
is not started if the check fails.
- return value - according to this check. true if the folder was be deleted, false otherwise.
function checkRightsRec($dir) {
$right = is_writable($dir);
if ($right && is_dir($dir)) {
$objects = scandir($dir);
$curObjRight = false;
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
$curObjRight = checkRightsRec($dir."/".$object);
if(!$curObjRight){
$right = false;
}
}
}
}
return $right;
}
function rrmdir($dir) {
if(!checkRightsRec($dir)){
return false;
}
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir")
rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
return true;
}