What is the difference between DirectoryIterator and FileSystemIterator?

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 , which documentation only gives this description:

  

The Filesystem iterator

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

For me, apparently, they both do the same thing, but documentation gives us the impression that they are two different things.

But, after all, what is the difference between them?

    
asked by anonymous 30.09.2016 / 16:44

3 answers

7

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

DirectoryIterator

  

The DirectoryIterator class implements the Iterator interface, it   has the methods to manipulate the "pointer" for the item traversed.   For example, you have the rewind method to go back to the first   position. Also, since you implement the SeekableIterator interface,   has the seek method, which moves the pointer to a desired position.


FilesystemIterator

  

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

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


GlobIterator

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

  

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


Usage recommendation

The use of these classes is recommended because they offer the same features as opendir , readdir and closedir (and some new ones), and is aligned with the Object Oriented model, PHP has been hiking. The only disadvantage is incompatibility with older versions of PHP (lower than version 5).


Difference

DirectoryIterator is an extension of SplFileInfo and FilesystemIterator is an extension of DirectoryIterator . And both 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've removed the examples from this stackoverflow answer .

Read the complete article at: Browse Directories and Files with PHP .

    
03.10.2016 / 22:34
1

The answer is in the first comment on the FileSystemIterator page.

  

When you iterate using DirectoryIterator each "value" returned is the same DirectoryIterator object. The internal state is changed 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" argument 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 you have iterating you would get an integer value.

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

    
02.12.2016 / 18:45
1

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

DirectoryIterator

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

FileSystemIterator

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

See below for 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

An important point, not mentioned in other answers, is that FileSystemIterator , in addition to following symbolic links, also does not list points ( . and .. ) part of the file system, and this is because the FilesystemIterator::SKIP_DOTS flag is enabled by default in construtor .

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

See more about iterators:

21.06.2017 / 18:34