W3docs

closedir()

The directory_closedir function in PHP is a valuable function that helps you to release the system resources that have been allocated to the directory stream.

Introduction

The closedir function in PHP is a valuable function that helps you to release the system resources that have been allocated to the directory stream. This function is essential to prevent memory leaks and ensure that your code runs smoothly. However, it's essential to understand the correct usage of this function to prevent any unexpected behavior in your code.

Understanding closedir function

The closedir() function closes a directory handle that was previously opened by opendir(). Once the handle is closed, you can no longer read entries from it with readdir() until you reopen the directory.

The syntax of PHP closedir() function

closedir(resource $dir_handle): void
  • $dir_handle — the directory handle returned by opendir(). This parameter is optional: if omitted, PHP closes the last directory handle opened by opendir().

The function returns no value (void). It simply releases the underlying system resource, so there is nothing to capture in a variable.

Closing a directory handle

When you are working with a large number of files, it's essential to close the directory handle to release the system resources. If you don't close the directory handle, your code will consume more and more memory, which could eventually lead to resource exhaustion.

To close a directory handle, you need to use the closedir function. The function takes a single parameter, which is a resource representing the directory handle that you want to close.

A typical workflow is: open the directory, read its entries, then close the handle. Here's a complete, runnable example that lists the current directory and always closes the handle:

Example of closedir() function in PHP

<?php

$dir_handle = opendir('.');

if ($dir_handle !== false) {
    while (($entry = readdir($dir_handle)) !== false) {
        echo $entry . "\n";
    }
    closedir($dir_handle);
}

Running this on a directory that contains index.php and style.css prints something like:

.
..
index.php
style.css

We first open the directory with opendir(), read each entry with readdir() in a loop, and finally release the handle with closedir().

Always closing the handle with try...finally

If the code that processes entries can throw an exception, wrap it in a try...finally block so the handle is closed even when something goes wrong:

<?php

$dir_handle = opendir('/path/to/directory');

if ($dir_handle !== false) {
    try {
        // Do something with the directory handle that might throw
        while (($entry = readdir($dir_handle)) !== false) {
            // process $entry
        }
    } finally {
        closedir($dir_handle);
    }
}

The finally block runs whether the loop completes normally or an exception is thrown, guaranteeing the resource is released.

Common mistakes with closedir

One common mistake when working with closedir is trying to close a directory handle twice. Once you have closed a directory handle, you cannot close it again. Attempting to do so will trigger a warning.

Another common mistake is not checking whether the directory handle is valid before closing it. opendir() returns false on failure, so always confirm the handle is not false (as in the examples above) before passing it to closedir(). Passing an invalid handle triggers a TypeError or warning depending on your PHP version.

A third pitfall is forgetting that closing the handle does not delete or modify the directory — it only frees the resource used to iterate over it. The files on disk are untouched.

  • opendir() — opens a directory handle for reading.
  • readdir() — reads the next entry from an open directory handle.
  • rewinddir() — resets the handle back to the start of the directory.
  • scandir() — returns all directory entries as an array in one call (no manual handle to close).

Conclusion

The closedir() function is essential for releasing system resources allocated to a directory stream. Using it correctly—by verifying the handle's validity and wrapping operations in a try...finally block—prevents warnings and memory leaks. Following these guidelines ensures your PHP code runs efficiently and reliably.

Practice

Practice
What does the PHP function closedir() do?
What does the PHP function closedir() do?
Was this page helpful?