PHP Directory Functions

by Vincy. Last modified on June 2nd, 2021.

Some days before we have seen set of basic PHP file functions to perform file open, read, write or append operations. Similarly, PHP includes set of directory functions to deal with the operations, like, listing directory contents, and create/ open/ delete specified directory and etc. These basic functions are listed below.

  • mkdir(): To make new directory.
  • opendir(): To open directory.
  • readdir(): To read from a directory after opening it.
  • closedir(): To close directory with resource-id returned while opening.
  • rmdir(): To remove directory.

In this article, we have to discuss each of the above basic directory functions in PHP with their corresponding usage of these functions, possible parameter to be passed if any, with suitable examples.

Creating New Directory

For creating a new directory using PHP programming script, mkdir() function used, and, the usage of this function is as follows.


This function accepts four arguments as specified. Among them, the first argument is mandatory, whereas, the remaining set of arguments are optional.

  • $directory_path: By specifying either relative and absolute path, a new directory will be created in such location if any, otherwise, will return an error indicating that there are no such locations.
  • $mode: The mode parameter accepts octal values in which the accessibility of the newly created directory depends on.
  • $recursive: This parameter is a flag and having values either true or false, that allow or refuse to create nested directories further.
  • $context: As similar as we have with PHP unlink() having a stream for specifying protocols and etc.

This function will return boolean data, that is, true on successful execution, false otherwise.

Listing Directory Content in PHP

For listing the contents of a directory, we require two of the above-listed PHP directory functions, these are, opendir() and readdir(). There are two steps in directory listing using PHP program.

  • Step 1: Opening directory.
  • Step 2: Reading content to be listed one by one using PHP loop.

Step 1: Opening Directory Link

As its name, opendir() function is used to perform this step. And, it has two arguments, one is compulsory for specifying the path of the directory, and the other is optional, expecting stream context if any. The syntax will be,


Unlike PHP mkdir() returning boolean value, this function will return resource data as like as fopen(), mysql_query() and etc. After receiving the resource identifier return by this function, then only we can progress with the subsequent steps to read, rewind or to close required directory with the reference of this resource id.

Otherwise, PHP error will occur for indicating the user, that the resource id is not valid.

Step 2: Reading Directory Content

For performing this step, we need to call readdir() function recursively until the directory handle reaches the end of the directory. For that, we need to specify the resource-id returned while invoking opendir(), indicated as directory handle.

PHP readdir() will return string data on each iteration of the loop, and this string will be the name of each item stored in the directory with its corresponding extension. For example,

$directory_handle = opendir($directory_path);
while($directory_item = readdir($directory_handle)) {
echo $directory_item . "<br>";

And, thereby executing the above code sample, we can list the content of a directory as expected.

Closing Directory Link

Once the directory link is opened to perform set of dependent operations like reading directory content, we need to close this link after completing the related functionalities required. For example,

$directory_handle = opendir($directory_path);

Removing Directory

We have seen in the previous article about how to delete a file from a directory using PHP unlink(). Similarly, for removing the entire directory, PHP provides a function named as rmdir() which accepts the same set of arguments, as like as mkdir(). These are, the $directory_path and $context(Optional) as stated below.


But, this function will remove the directory, if and only if it is empty. For removing the non-empty directory, we need to create a user define a function that recursively calls unlink() function to remove each file stored in the directory to be deleted.

Example: PHP Directory Functions:

The following PHP program deals with the set of directory functions, that is, for creating a new directory, open and read for listing directory content and closing directory as follows.

/*Creating files into php_directory_functions_manual*/
$file_pointer1 = fopen("php_directory_functions_manual/mkdir.txt","x");
$file_pointer2 = fopen("php_directory_functions_manual/rmdir.txt","x");
$directory_handle = opendir("php_directory_functions_manual");
while($directory_item = readdir($directory_handle)) {
echo $directory_item . "<br>";

This program will return the list of created files by using fopen() function as shown below.


And then, let us have an another example, for looking into how a directory can be removed.

$directory_handle = opendir("php_directory_functions_manual");
while($directory_item = readdir($directory_handle)) {

On iterating with a loop for reading each item stored into the directory, the unlink() function is invoked to wipeout the directory before attempting to delete it. And then, rmdir() is used by referring with the name of the directory, to remove it.


  • If we invoked opendir() only once in a PHP program, its dependent functions, like, readdir(), closedir() and etc., will execute successfully, without specifying the directory handle resource, since, it refers recently returned resource by default. Rather, if we have multiple resource data referring various directory links, then we need to specify the directory handle appropriately.
  • For getting the successful execution of PHP unlink() and rmdir(), all required permissions should be provided with the entries on which these functions are applied.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top