
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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 (..).