PHP Directory readdir() Function
The PHP Directory readdir() function is used to find the names of the files or folders inside a directory. As the name suggests readdir stands for "Read Directory." This function basically reads the names of files or folders one at a time. The filesystem stores the order in which entries are returned. The function is called "readdir" because it reads content from a directory.
It returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.
As this function only returns one element at a time from the directory, you are likely thinking how to retrieve all the items at once in a directory if there are multiple files or folders present. To do this, you can either run a loop or use the scandir() method.
Syntax
Here is the syntax of the PHP Directory readdir() function −
string readdir ( resource $dir_handle );
Parameters
The parameters are needed to use the readdir() function are mentioned below −
| Sr.No | Parameter & Description |
|---|---|
| 1 |
dir_handle(Required) The directory handle resource previously opened with opendir(). |
Return Value
It returns the filename on success, or FALSE on failure.
PHP Version
First appeared in core PHP 4, the readdir() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example
In this example we will show you how we can use use the PHP Directory readdir() function after opening the directory using opendir() function using while loop.
<?php
$dir = opendir("/var/www/images");
while (($file = readdir($dir)) !== false) {
echo "filename: " . $file . "<br />";
}
closedir($dir);
?>
Output
This will produce the following result −
filename: . filename: .. filename: logo.gif filename: mohd.gif
Example
Previously we have seen how we can retrieve files using while loop and readdir() function now we will see how we can display file names using if-else statement.
<?php
// declare the directory path (You can replace it with your specific directory path)
$directory = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";
if(is_dir($directory)){
if($dir_handle = opendir($directory)){
while(false !== ($entry = readdir($dir_handle))){
echo "My Directory Content is: " . $entry . "<br>";
}
}
}else{
echo "This is not a directory.";
}
?>
Output
This will lead to the following outcome −
My Directory Content is: . My Directory Content is: .. My Directory Content is: .DS_Store My Directory Content is: Pictures My Directory Content is: my_folder My Directory Content is: index.txt My Directory Content is: my.txt
Example
In the last example you can see that in the output there is . or .. present so in this example we will remove them for better view of files or folders. To have this functionality we will just use if condition.
<?php
// declare the directory path (You can replace it with your specific directory path)
$directoryPath = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";
if(is_dir($directoryPath)){
if($handle = opendir($directoryPath)){
while(false !== ($item = readdir($handle))){
if ($item != "." && $item != "..") {
echo "Item in Directory: " . $item . "<br>";
}
}
closedir($handle); // Close the directory handle
}
}else{
echo "The specified path is not a directory.";
}
?>
Output
The result of this PHP code is −
Item in Directory: .DS_Store Item in Directory: Pictures Item in Directory: my_folder Item in Directory: index.txt Item in Directory: my.txt
Example
As we have seen different examples of how we can use readdir() function to read the entities present in the directory. Now we will only read the files and leave other content line images and folders present.
<?php
// declare the directory path (You can replace it with your specific directory path)
$directory = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";
// check if the specified path is a directory
if(is_dir($directory)){
// open the directory
if($dir_handle = opendir($directory)){
// Loop over each entry in the directory
while(false !== ($entry = readdir($dir_handle))){
// check if the entry is a file
if (is_file($directory . "/" . $entry)) {
// echo the name of the file
echo "File in Directory: " . $entry . "<br>";
}
}
// close the directory handle
closedir($dir_handle);
}
} else {
// echo an error message if the specified path is not a directory
echo "The specified path is not a directory.";
}
?>
Output
This will produce the below output −
File in Directory: .DS_Store File in Directory: index.txt File in Directory: my.txt
Note
When you call the readdir() method two different items appear: one dot (.) for the current directory and two dots (..) for the parent directory. The parent directory is the folder that is now directly above this one. In general, you will not need to use these particular items when working with the files in the directory.
Summary
PHP's readdir() method is used to read the names of files and directories one at a time inside a directory. The next file or folder name in the directory is given back. This approach is useful for listing directory contents sequentially; it includes special entries for the current directory (.) and parent directory (..).