Categories
PHP

Recursive Directory Iterator

How to use DirectoryIterator and RecursiveDirectoryIterator classes to loop through all files and folders inside a directory.

Using a DirectoryIterator to Find Files in a Folder

Iterate is a term for looping, a DirectoryIterator class can loop through files in folders (directories) and RecursiveDirectoryIterator class can loop through files in directories recursively. Here’s an example:

Example: DirectoryIterator using while loop

Use the while loop with DirectoryIterator::valid() and DirectoryIterator::next(), the while loop repeats a code block as many times as there are items in a directory:

<?php
 $dir = 'uploads';
 $files = new DirectoryIterator( $dir );
 while ( $files->valid() ) {
  echo $files->current().'<br>';
  $files->next();
 }

The valid() method of the DirectoryIterator object returns true if it currently points to a valid item in the directory. The next() method cause the DirectoryIterator object to point to the next item in the directory.

Example: DirectoryIterator using foreach loop

<?php
 $dir = 'uploads';
 $files = new DirectoryIterator( $dir );
 foreach ($files as $file) {
  if (!$file->isDot())
   echo $file.'<br>';
 }

The isDot() method identifies the special files . (current directory) and .. (parent directory).

Example: Using isDir(), isFile(), and isLink() etc.

<?php
 $dir = 'uploads';
 $files = new DirectoryIterator( $dir );
 foreach ($files as $file) {
  if ($file->isDir())
   echo 'Directory: '. $file.'<br>';
  else if ($file->isFile())
   echo 'File: '. $file.'<br>';
  else if ($file->isLink())
   echo 'Symlink: '. $file.'<br>';
  else
   echo 'Other: '. $file.'<br>';
 }
  • The isDir() method determines if the current item is a directory.
  • The isFile() method determines if the current item is a regular file.
  • The isLink() method determines if the current item is a symbolic link.

See, https://php.net/manual/class.directoryiterator.php.

Iterating recursively over directories with RecursiveDirectoryIterator

The DirectoryIterator class iterate over a single directory, use the RecursiveDirectoryIterator class if you need to find files down into subdirectories. This class automatically enters any subdirectory and iterates through its contents. To iterate over a directory recursively, you need to wrap RecursiveDirectoryIterator object in the RecursiveIteratorIterator object.

<?php
 $dir = new RecursiveDirectoryIterator('uploads');
 $files = new RecursiveIteratorIterator($dir);
 foreach ($files as $file) {
  echo $file . '<br>';
 }
/*Prints:
D:\xampp\htdocs\.
D:\xampp\htdocs\..
D:\xampp\htdocs\.htaccess
D:\xampp\htdocs\3d\.
D:\xampp\htdocs\3d\..
....
*/

To access only the filenames use getFilename() methods:

<?php
 $dir = new RecursiveDirectoryIterator(__DIR__);
 $files = new RecursiveIteratorIterator($dir);
 foreach ($files as $file) {
  echo $file->getFilename() . '<br>';
 }
/* Prints:
.
..
.htaccess
.
..
cycles-renderer-reflection-yt.html
.
..
12047a40.png
*/

Example: using flags to skip current . and parent directory ..

<?php
 $flags = FilesystemIterator::SKIP_DOTS;
 $dir = new RecursiveDirectoryIterator('uploads', $flags);
 $files = new RecursiveIteratorIterator($dir);
 foreach ($files as $file) {
  echo $file->getFilename() . '<br>';
 }
/* Prints:
.htaccess
cycles-renderer-reflection-yt.html
12047a40.png
*/

See, https://php.net/manual/class.recursivedirectoryiterator.php.


Working with Files in PHP: