note 124816 added to directoryiterator.construct
| From: | ahmad dot mayahi at gmail dot com | Date: | Thu, 12 Mar 2020 06:57:42 +0000 |
| Subject: | note 124816 added to directoryiterator.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214316@lists.php.net to get a copy of this message | ||
Many developers get confused between DirectoryIterator and FilesystemIterator, there are a few small
differences that I'm goning to list them here:
DirectoryIterator:
* Includes the "." and ".." within the path.
* Numberd keys.
* Path is not included in in value.
* No configuration available.
* You do need to use "clone" when storing the elements into an array.
Example:
<?php
$files = [];
foreach ($dir as $key => $item) {
echo $key.PHP_EOL; // The key is a number
$files[] = $item;
}
echo $files[0]->getFilename(); // nothing happens here, you do need to use clone $item inside the
foreach
?>
FilesystemIterator:
* Dot files skipped.
* Pathname is used as a key as well as a value.
* Configurable.
* Uses SplFileInfo to store the files.
"FilesystemIterator" it's an enhanced version of DirectoryIterator, and I prefer to
use it instead of the DirectoryIterator.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.195 (proxied: 109.57.42.245)
----
Manual Page -- http://php.net/manual/en/directoryiterator.construct.php
Edit -- https://master.php.net/note/edit/124816
Del: integrated -- https://master.php.net/note/delete/124816/integrated
Del: useless -- https://master.php.net/note/delete/124816/useless
Del: bad code -- https://master.php.net/note/delete/124816/bad+code
Del: spam -- https://master.php.net/note/delete/124816/spam
Del: non-english -- https://master.php.net/note/delete/124816/non-english
Del: in docs -- https://master.php.net/note/delete/124816/in+docs
Del: other reasons-- https://master.php.net/note/delete/124816
Reject -- https://master.php.net/note/reject/124816
Search -- https://master.php.net/manage/user-notes.php