PHP DirectoryIterator
DirectoryIterator is a built-in class in PHP that provides an object-oriented way to interact with directories. This class allows you to easily iterate over directories and files.
The DirectoryIterator object provides a very simple interface for directory traversal in PHP. It allows you to check file names, extensions, paths, and attributes in an object-oriented manner.
Since this is part of the Standard PHP Library (SPL), DirectoryIterator makes directory handling more readable, flexible, and easier to maintain compared to traditional methods.
Basic Syntax of DirectoryIterator in PHP
The basic syntax of DirectoryIterator in PHP is as follows:
$iterator = new DirectoryIterator(string $directory);In the above syntax,
- $directory → It specifies the path of the directory you want to iterate.
- $iterator → This is an object of the DirectoryIterator class. You can use this object to loop through the files and subdirectories inside the directory.
Once the object is created, you can use it in a foreach loop to access file and directory information.
Methods of DirectoryIterator Class
DirectoryIterator is a class in the Standard PHP Library that provides several useful methods, including:
- getFilename() → Returns the name of the file or directory.
- getExtension() → Returns the file extension.
- isFile() / isDir() → Checks whether the item is a file or a directory.
- getPathname() → Returns the full path of the file or directory.
- getSize() → Returns the size of the file in bytes.
- getMTime() → Returns the last modified time of the file.
Examples of PHP DirectoryIterator
Now, let’s explore some practical examples of using the DirectoryIterator object in PHP.
Example 1: Listing all files and directories using DirectoryIterator
<?php
// Create a DirectoryIterator object for the given path.
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
// Loop through the directory contents.
foreach ($dir as $fileinfo) {
echo $fileinfo->getFilename() . "\n";
}
?>Output:
.
..
index.php
style.css
images
In this example, the iteration lists all files and directories, including . (current directory) and .. (parent directory). To skip these, we can add a condition.
Example 2: Skip . and ..
<?php
// Creates a new DirectoryIterator object for the directoty located at the specified path.
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
echo $fileinfo->getFilename() . "\n";
}
}
?>Output:
index.php
style.css
images
Explanation:
1. $dir = new DirectoryIterator(“D:/xampp/htdocs/PHP_Project”); creates a new DirectoryIterator object for the directory located at
D:/xampp/htdocs/PHP_Project. This object allows you to loop through all the files and subdirectories inside that folder.
2. foreach ($dir as $fileinfo) iterates through each item in the directory. For every file or folder, PHP creates a DirectoryIterator object stored in $fileinfo.
3. if (!$fileinfo->isDot()) checks if the item contains . (current directory) or .. (parent directory). Since these are not actual files or folders, the condition !$fileinfo->isDot() will skip them.
Example 3: Check File or Directory
<?php
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile()) {
echo $fileinfo->getFilename() . " is a file. \n";
} elseif ($fileinfo->isDir()) {
echo $fileinfo->getFilename() . " is a directory. \n";
}
}
?>Output:
index.php is a file.
style.css is a file.
images is a directory.
Explanation:
(1) In this example, the isFile() method checks whether the current item in the directory is a file. It returns true if the item is a file, otherwise returns false.
(2) The getFilename() method returns the name of the current file or directory as a string.
(3) The isDir() method checks whether the current item in the directory is a directory. It returns true if the item is a directory. Otherwise, it returns false.
Example 4: Filter Files by Extension
Suppose you want only PHP files:
<?php
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile() && $fileinfo->getExtension() === "php") {
echo $fileinfo->getFilename() . "\n";
}
}
?>Output:
index.php
config.php
test.php
Example 5: Get File Details (Size, Path, Last Modified)
<?php
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile()) {
echo "File Name: " . $fileinfo->getFilename() . "\n";
echo "Size: " . $fileinfo->getSize() . " bytes \n";
echo "Path: " . $fileinfo->getPathname() . "\n ";
echo "Last Modified: " . date("Y-m-d H:i:s", $fileinfo->getMTime()) . "\n";
}
}
?>Output:
File Name: index.php
Size: 2450 bytes
Path: D:/xampp/htdocs/PHP_Project/index.php
Last Modified: 2025-09-22 12:14:55
Example 6: Iterate Only Directories
<?php
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo $fileinfo->getFilename() . "\n";
}
}
?>Output:
images
uploads
css
PHP isDot() Method
DirectoryIterator class provides a isDot() method, which checks whether the current directory entry is one of the special directories:
- . (dot) → Refers to the current directory.
- .. (double dot) → Refers to the parent directory.
If the current entry is either a current directory or parent directory, the method returns true. Otherwise, it returns false. Its basic syntax is given below:
$object->isDot(): boolExample: Checking Directory
<?php
$dir = new DirectoryIterator("D:/xampp/htdocs/PHP_Project");
foreach ($dir as $fileinfo) {
if ($fileinfo->isDot()) {
echo $fileinfo->getFilename() . " is a dot entry. \n";
} else {
echo $fileinfo->getFilename() . " is a valid file/directory. \n";
}
}
?>Output:
. is a dot entry.
.. is a dot entry.
index.php is a valid file/directory.
style.css is a valid file/directory.
images is a valid file/directory.Conclusion
DirectoryIterator is a class in the Standard PHP Library that allows easy and object-oriented way to iterate directories. Compared to traditional functions like opendir() and scandir() functions, DirectoryIterator is more powerful, cleaner, and easier to maintain. For iterating through nested directories, you can use RecursiveDirectoryIterator.



