PHP readdir() Function

What is PHP readdir() Function?

If you want to get the files/folders name from a directory, use readdir() function. This function reads one file/folder name at a time. It returns entries in the order they are stored by the filesystem. The function name “readdir” is short for “Read Directory” as the function read content of a directory.

As this function returns just one entry from the directory at a time, you may ask if there exists multiple files or folders in a directory, how we’ll retrieve all the entries at once? To do this, you can run a loop or use scandir() function.

Syntax:

readdir(dir_handle)

Parameters:

The Function has 1 parameter which is optional-

dir_handle (Optional): It is a directory handle that was opened previously by the opendir() function. If you don’t mention the parameter, it uses the last opened directory handle by opendir() function. Check example 2.

Return Values:

The function returns-

  • File or folder name – on success. The parameter (directory handler) is a resource id and a resource is considered as TRUE. When it is TRUE, the function returns the file/folder name.
  • FALSE – on failure.

Examples:

Example 1: Listing all the entries in a folder-

<?php
if(is_dir("./file/")){
    if($dir_handle = opendir("./file/")){
        while(false !== ($entry = readdir($dir_handle))){
            echo "Directory Content: " . $entry . "<br />";
        }
    }
}else{
    echo "It's not a directory.";
}
?>

Output:

Directory Content: .
Directory Content: ..
Directory Content: Function.docx
Directory Content: docs
Directory Content: top.jpg

Explanation:

Line 2: is_dir() function check whether the /file/ is a directory or not.

Line 3: As /file/ is a valid directory path here, the opendir() function opens the directory and returns the directory handle to the $dir_handle variable.

Line 4: The readdir() function reads a directory entry (ex. file, folder). Each time the loop calls readdir(), the function returns the next entry in the directory. Once it reaches the end of the directory, it will return false.

Example 2: Listing all the entries in a directory without mentioning directory handle-

<?php
if(is_dir("./file/")){
    if($dir_handle = opendir("./file/")){
        while(false !== ($entry = readdir())){
            echo "Directory Content: " . $entry . "<br />";
        }
    }
}else{
    echo "It's not a directory.";
}
?>

Output:

Directory Content: ..
Directory Content: Function.docx
Directory Content: docs
Directory Content: top.jpg

Explanation:

Line 2: is_dir() function check whether the /file/ is a directory or not.

Line 3: As /file/ is a valid directory path here, the opendir() function opens the directory and returns the directory handle to the $dir_handle variable.

Line 4: The readdir() function reads a directory entry (ex. file, folder). Each time the loop calls readdir(), the function returns the next entry in the directory.  Once it reaches the end of the directory, it will return false.

Note: Here, there is no directory handle in the readdir() function but it still works because it uses a previously opened directory handle.

Example 3: Listing all the entries in a folder without . and ..-

<?php
if(is_dir("./file/")){
    if($dir_handle = opendir("./file/")){
        while(false !== ($entry = readdir($dir_handle))){
            if ($entry != "." && $entry != "..") {
                echo "Directory File/Folder: " . $entry . "<br />";
            }
        }
    }
}else{
    echo "It's not a directory.";
}
?>

Output:

Directory File/Folder: Function.docx
Directory File/Folder: New folder
Directory File/Folder: top.jpg

Explanation:

Line 2: is_dir() function check whether the /file/ is a directory or not.

Line 3: As /file/ is a valid directory path here, the opendir() function opens the directory and returns the directory handle to the $dir_handle variable.

Line 4: The readdir() function reads a directory entry (ex. file, folder). Each time the loop calls readdir(), the function returns the next entry (ex. file/folder) in the directory.  Once it reaches the end of the directory, it will return false.

Line 5: We discard the current directory (.) and its parent directory (..) by the condition.

Notes on readdir() Function:

  • In the readdir() function output, you see single dot (.) and double dot(..) as output. Here, single dot means the working directory that you’re using and double dot means parent directory of the working directory which is immediately above the working directory.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP readdir() Function

readdir() function is one of the indispensable function when working with PHP directory system. readdir(), which is one of the directory functions in PHP, helps to read the content inside a directory.

Reference:

https://www.php.net/manual/en/function.readdir.php