note 107223 added to class.recursivedirectoryiterator
| From: | rokclimb15 at gmail dot com | Date: | Tue, 17 Jan 2012 19:59:48 +0000 |
| Subject: | note 107223 added to class.recursivedirectoryiterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185682@lists.php.net to get a copy of this message | ||
To improve on joelhy's example:
This will perform the delete skipping . and .. to avoid trying to delete them before they are truly
empty. This is ideal for nested dir structures containing files at every level. At the end, delete
the parent dir.
<?php
if (is_dir($dir)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir,
FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $path) {
if ($path->isDir()) {
rmdir($path->__toString());
} else {
unlink($path->__toString());
}
}
// dir is empty now, remove it
rmdir($dir);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 69.46.155.252
----
Manual Page -- http://www.php.net/manual/en/class.recursivedirectoryiterator.php
Edit -- https://master.php.net/note/edit/107223
Del: integrated -- https://master.php.net/note/delete/107223/integrated
Del: useless -- https://master.php.net/note/delete/107223/useless
Del: bad code -- https://master.php.net/note/delete/107223/bad+code
Del: spam -- https://master.php.net/note/delete/107223/spam
Del: non-english -- https://master.php.net/note/delete/107223/non-english
Del: in docs -- https://master.php.net/note/delete/107223/in+docs
Del: other reasons-- https://master.php.net/note/delete/107223
Reject -- https://master.php.net/note/reject/107223
Search -- https://master.php.net/manage/user-notes.php