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:
- Returning or Downloading Files with an HTTP Request
- Reading a File into a String or Array
- PHP Opening and Closing Files
- Reading files by line or by character
- Writing and appending to files
- Reading and Writing CSV Files
- Parsing INI Files and Strings
- Check File Type (whether a file is a directory or a file)
- Understanding file Permissions in PHP
- Reading Information About Files in PHP
- Copying, Moving and Deleting Files in PHP
- Reading Directories Contents in PHP
- Browse directories recursively
- Zipping and Unzipping a File with Zlib and Bzip2
- Zip and Unzip Archives with ZipArchive
- Using Relative Paths for File Access