PHP opendir() Function

What is PHP opendir() Function?

The opendir() function opens a directory of a specific directory path. Just mention a specific directory path and the function opens it by returning you a resource id which is a directory handle. You can use this directory handle to traversing all the files and folders inside the directory and perform other related operations like read files & folders, change directory etc using other functions. “opendir” is short for “Open Directory”.

Syntax:

opendir(directory, context)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

directory (Required): The directory that you want to open.

context (Optional): A context stream resource. It allows you to define context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Values:

The functions-

  • Directory handle (resource id) – on success.
  • FALSE – on failure.

Examples:

Example 1:

<?php
$dir_handle = opendir("./file/");
if(is_resource($dir_handle)){
    echo "It is a resource and it is: " . $dir_handle;
}
?>

Output:

It is a resource and it is: Resource id #3

Explanation:

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

Line 3: As the $dir_handle variable is a resource variable, the PHP interpreter prints the next line.

Example 2:

<?php
if($dir_handle = opendir("./file/")){
    while($file=readdir($dir_handle)){
        echo "Files/folders are: " . $file . "<br />";
    }
}
?>

Output:

Files/folders are: .
Files/folders are: ..
Files/folders are: doc
Files/folders are: 1.jpg

Explanation:

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

Line 3: 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.

Note: For simplicity, we checked the above condition in the while loop. To see the correct way of checking it, check example of readdir() function.

Example 3:

<?php
if(is_dir("./file/")){
    echo "It's a directory.";
}else{
    echo "It's not a directory.";
}
?>

Output:

It’s a directory.

Explanation:

Line 2: is_dir() function check whether the /file/ is a directory is directory or not. As it is a directory, the script prints the above output.

Caution:

You may get errors after running the function for the following reasons-

  • You may mention a wrong directory path. Check example 3.
  • The directory may not have permission to access.
  • The file system doesn’t allow to open the directory that you’re trying to access.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP opendir() Function

When working with files and folders in PHP, you’ll need to open the directory at first and opendir() is the function that does it which is one of the directory functions in PHP.

Reference:

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