PHP closedir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, closedir() closes a directory handle that was opened with opendir(). It tells PHP you are finished reading the folder.

When you open a folder with opendir(), PHP keeps a connection to it open. closedir() releases that connection. It is good practice to call it as soon as you are done with readdir(), just like you call fclose() for files. It always returns true.

Syntax

Call closedir() with the values you need. Start with this line.

PHP
closedir($handle);
CallWhat it does
closedir($handle)Closes the directory handle stored in $handle.
$handle = opendir($folder); closedir($handle);Opens a folder and closes it again right away.
closedir()Closes the most recently opened handle. This works, but passing the handle is clearer and recommended.

Always pass the handle you got from opendir(). Calling closedir() with no argument relies on the last opened directory, which is confusing and is deprecated in PHP 8.5 and later.

Library declarations
PHP
closedir(?resource $dir_handle = null): true

Parameters

ParameterDescription
dir_handleOptional. The directory handle returned by opendir(). If it is left out or null, PHP uses the last directory handle that was opened, but you should always pass the handle explicitly.

Return Value

Return ValueDescription
trueAlways returns true. In older PHP versions this function had no useful return value.

PHP Examples

Example 1 — Opening and Closing a Directory Handle

We open an empty demo folder, check the handle, close it, and check it again to see what closedir() changed.

PHP
<?php
$folder = __DIR__ . '/demo-closedir';
mkdir($folder);

$handle = opendir($folder);
echo "Before closedir: ", gettype($handle), PHP_EOL;
echo "Is open: ", is_resource($handle) ? 'yes' : 'no', PHP_EOL;

closedir($handle);
echo "After closedir: ", gettype($handle), PHP_EOL;
echo "Is open: ", is_resource($handle) ? 'yes' : 'no', PHP_EOL;

rmdir($folder);

How It Works

  1. mkdir() creates an empty demo folder so the output is the same every time.
  2. opendir() returns a handle, and gettype() reports it as a resource.
  3. closedir($handle) closes the handle, and gettype() now says 'resource (closed)'.
  4. is_resource() returns false for a closed handle. We then remove the demo folder with rmdir().

Example 2 — Using a Handle After It Is Closed

A closed handle cannot be used again. Trying to read from it throws a TypeError that you can catch.

PHP
<?php
$folder = __DIR__ . '/demo-closedir-edge';
mkdir($folder);

$handle = opendir($folder);
closedir($handle);

try {
    readdir($handle);
    echo "Read worked", PHP_EOL;
} catch (TypeError $e) {
    echo "Caught TypeError: handle is closed", PHP_EOL;
}

rmdir($folder);

How It Works

  1. We open the folder and immediately close the handle.
  2. readdir($handle) now receives a closed resource, so PHP throws a TypeError.
  3. The catch block prints our own message instead of letting the script stop with a fatal error.
  4. To read the folder again, you would simply call opendir() once more.

Example 3 — A Helper Function That Always Closes

A neat habit is to wrap open, read and close in one function, so you can never forget closedir().

PHP
<?php
function countFiles(string $folder): int
{
    $handle = opendir($folder);
    $count = 0;

    while (($entry = readdir($handle)) !== false) {
        if ($entry !== '.' && $entry !== '..') {
            $count++;
        }
    }

    closedir($handle);
    return $count;
}

$folder = __DIR__ . '/demo-closedir-count';
mkdir($folder);
foreach (['a.txt', 'b.txt', 'c.txt'] as $name) {
    file_put_contents($folder . '/' . $name, 'x');
}

echo "Files: ", countFiles($folder), PHP_EOL;

foreach (['a.txt', 'b.txt', 'c.txt'] as $name) {
    unlink($folder . '/' . $name);
}
rmdir($folder);

How It Works

  1. countFiles() opens the folder, loops through every entry with readdir(), and counts them.
  2. The entries '. ' and '..' are skipped because they are not real files.
  3. closedir($handle) runs before the function returns, so the handle is always released.
  4. The demo creates three files, prints the count, and then removes everything it created.

Frequently Asked Questions

It closes a directory handle that was opened with opendir(). This releases the resource PHP was holding for that folder.
PHP closes open handles when the script ends, so small scripts will not break. Still, it is good practice to call closedir() as soon as you finish reading, especially in long running scripts or loops.
It returns true. Older versions of PHP did not return a useful value, so most code never checks the result.
PHP throws a TypeError because the resource is no longer valid. Open the folder again with opendir() to get a new handle.
Yes, PHP then closes the last opened directory handle. This is deprecated in PHP 8.5 and later, so always pass the handle that opendir() gave you.

Next: dir()

dir() opens a folder and returns a Directory object. It is a pseudo object-oriented way to.

dir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful