note 70714 added to function.rmdir
| From: | graced at monroe dot wednet dot edu | Date: | Tue, 24 Oct 2006 21:09:43 +0000 |
| Subject: | note 70714 added to function.rmdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119085@lists.php.net to get a copy of this message | ||
The previous example won't work if the directory contains 'hidden' files (those with
a leading dot e.g. .htaccess) -- glob() does not catch them. Also, it will follow symlinks and as a
result probably delete more than anticipated
The below function does not have either of those problems, and is functionally equivalent to
"rm -rf" with the exception that it must be called on a directory (changing that is an
excercise to a reader).
Trailing slashes are allowed but not required -- PHP doesn't care if you're talking about
/dir or /dir/ or /dir////.
<?php
function rmdir_recursive($dir) {
$dp = opendir($dir);
while(($fn = readdir($dp)) !== false) {
if($fn == '.' || $fn == '..') continue;
$fn = "{$dir}/{$fn}";
echo "{$fn}\n"; // optional.
if(is_dir($fn) && !is_link($fn)) rmdir_recursive($fn);
else unlink($fn);
}
closedir($dp);
rmdir($dir);
}
----
Server IP: 64.71.164.2
Probable Submitter: 152.157.87.197 (proxied: 10.19.0.152)
----
Manual Page -- http://www.php.net/manual/en/function.rmdir.php
Edit -- https://master.php.net/note/edit/70714
Del: integrated -- https://master.php.net/note/delete/70714/integrated
Del: useless -- https://master.php.net/note/delete/70714/useless
Del: bad code -- https://master.php.net/note/delete/70714/bad+code
Del: spam -- https://master.php.net/note/delete/70714/spam
Del: non-english -- https://master.php.net/note/delete/70714/non-english
Del: in docs -- https://master.php.net/note/delete/70714/in+docs
Del: other reasons-- https://master.php.net/note/delete/70714
Reject -- https://master.php.net/note/reject/70714
Search -- https://master.php.net/manage/user-notes.php