What is the difference between Directoryiterator and Filesystemiterator?

Asked

Viewed 939 times

8

In PHP, there is the class Directoryiterator.

The Directoryiterator class provides a simple interface for viewing the Contents of filesystem Directories.

There is also Filesystemiterator, that the documentation only gives this description:

The Filesystem iterator

In the class synopsis, it shows that FileSystemIterator inherits DirectoryIterator.

To me, apparently, they both do the same thing, but the documentation gives us the impression of being two different things.

But, after all, what’s the difference between them?

  • 2

    Something tells me that, for once, they are conceptually wrong, at least in the nomenclature :D

  • @bigown why would they be "conceptually wrong"? which one is wrong?

  • I just wrote "something tells me" :P But if directory is something that is part of a filing system, this heritage is weird... But I’m not saying it’s all wrong there, nor would I be able to say without understanding.

3 answers

8


The explanation of Rubens Ribeiro, author of blog PHP is very good:

Directoryiterator

The class DirectoryIterator implements the interface Iterator, she has the methods to manipulate the "pointer" for the driven item. For example, it has the method rewind to return to the first position. Also, how implements the interface SeekableIterator, has the method seek, which moves the pointer to a desired position.


Filesystemiterator

The class FilesystemIterator that extends the class DirectoryIterator, and offers additional resources. For example, inform binary flags to get some behaviors, such as:

  • Ignore the "." and ".."
  • Follow symbolic links
  • Specify the type of return of the method current (used in iterations with foreach), etc..


Globiterator

Although it is not in your question, I find it interesting to say that there is also the class GlobIterator.

The class GlobIterator extends the class FilesystemIterator and offers the additional feature of scrolling through items from a expression, as shown with the glob function. However, by some unknown reason, the iterator does not have a similar behavior to that proposed by the option GLOB_BRACE.


Recommendation for use

The use of these classes is recommended as they offer the same features as opendir, readdir and closedir (and some new), and is aligned with the Object-Oriented model, where the resources of the PHP has walked. The only drawback is the incompatibility with old versions of PHP (lower than version 5).


Difference

DirectoryIterator is an extension of SplFileInfo and the FilesystemIterator is an extension of DirectoryIterator. And the two implement Iterator, Traversable, SeekableIterator.

Example DirectoryIterator:

$it = new DirectoryIterator(__DIR__);
foreach ($it as $fileinfo) {
  if (!$fileinfo->isDot())
    var_dump($fileinfo->getFilename());
}

Example FilesystemIterator:

$it = new FilesystemIterator(__DIR__);
foreach ($it as $fileinfo) {
  echo $fileinfo->getFilename() . "\n";
}

I removed the examples from that reply stackoverflow.

Read the full article on: Browse Directories and Files with PHP.

2

DiretoryIterator and FileSystemIterator are two iterators belonging to the SPL library of PHP.

Directoryiterator

DirectoryIterator implements the interface SeekableIterator and extends SplFileInfo. This class provides a simple interface for viewing contents of file directories.

Filesystemiterator

The class FileSystemIterator inherits DirectoryIterator, but there is an increase in methods setFlags and getFlags, that can change the behavior of this class.

See below some of these flags:

CURRENT_AS_PATHNAME
CURRENT_AS_FILEINFO
CURRENT_AS_SELF
CURRENT_MODE_MASK

KEY_AS_PATHNAME
KEY_AS_FILENAME

FOLLOW_SYMLINKS
KEY_MODE_MASK
NEW_CURRENT_AND_KEY
SKIP_DOTS
UNIX_PATHS

One important point, which were not mentioned in other answers, is that FileSystemIterator, in addition to following symbolic links, also nonlist the points (. and ..) as being part of the file system, and this is because the flag FilesystemIterator::SKIP_DOTS is activated by default on construtor.

The flags prefixed by CURRENT_AS_ is intended to change the function return FilesystemIterator::current (which is part of the implementation of SeekableIterator or Iterator), and those prefixed by KEY_AS_ changes the behavior of what is returned in the method FilesystemIterator::key. Thus, the argument $key of foreach will probably be affected, since it internally uses these methods to return iteration values when used with an implementation of Iterator.

See more about iterators:

1

The answer is in the first comment on the Filesystemiterator.

When you iterate using Directoryiterator each "value" returned is the same Directoryiterator Object. The Internal state is changed so when you call isDir(), getPathname(), etc the correct information is returned. If you Were to Ask for a key when iterating you will get an integer index value.

Filesystemiterator (and Recursivedirectoryiterator) on the other hand Returns a new, Different Splfileinfo Object for each iteration step. The key is the full pathname of the file. This is by default. You can change what is returned for the key or value using the "flags" arguement to the constructor.

And I translate, with adaptations:

"When you iterate using DirectoryIterator each "value" returned is the same object DirectoryIterator. The internal state is changed, so when you call isDir(), getPathname(), etc, the correct value is returned. If you were to use the keys when iterating you would get an integer value.

FileSystemIterator on the other hand returns a new object SplFileInfo for every step of the interaction. The key, also different, is the complete file path. This by default. You can change what is returned by the key using the "flags" argument in the constructor."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.