PHP opendir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, opendir() opens a folder and returns a directory handle. You then pass that handle to readdir() to list the entries, and to closedir() when you are finished.

opendir() is the first step of the classic way to read a folder in PHP: open it, loop with readdir(), then close it. It returns false and raises a warning if the folder cannot be opened, so always check the result.

Syntax

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

PHP
$handle = opendir($folder);
CallWhat it does
opendir($folder)Opens the folder and returns a handle, or false if it fails.
opendir(__DIR__)Opens the folder that holds the current script.
opendir('.')Opens the current working directory.

opendir() only opens the folder. It does not list anything by itself. If you simply want an array of names, scandir() is shorter and easier for beginners.

Library declarations
PHP
opendir(string $directory, ?resource $context = null): resource|false

Parameters

ParameterDescription
directoryRequired. The path of the folder to open.
contextOptional. A stream context created with stream_context_create(). Most scripts leave it out, and the default is null.

Return Value

Return ValueDescription
resource|falseReturns a directory handle on success. Returns false on failure, for example when the path is not a folder or cannot be read, and PHP also raises an E_WARNING.

PHP Examples

Example 1 — Listing the Files in a Folder

We create a demo folder with three files and use opendir(), readdir() and closedir() together to list them.

PHP
<?php
$folder = __DIR__ . '/demo-opendir';
mkdir($folder);
foreach (['red.txt', 'green.txt', 'blue.txt'] as $name) {
    file_put_contents($folder . '/' . $name, 'x');
}

$handle = opendir($folder);
$names = [];
while (($entry = readdir($handle)) !== false) {
    if ($entry !== '.' && $entry !== '..') {
        $names[] = $entry;
    }
}
closedir($handle);

sort($names);
echo implode(', ', $names), PHP_EOL;

foreach ($names as $name) {
    unlink($folder . '/' . $name);
}
rmdir($folder);

How It Works

  1. The demo creates three small files, so the result is the same on every run.
  2. opendir($folder) returns the handle, and readdir($handle) returns one name at a time.
  3. We ignore '. ' and '..' and then sort the names, because the file system may return them in any order.
  4. closedir() releases the handle, and the demo deletes everything it created.

Example 2 — Empty Folders and Missing Folders

An empty folder still contains '.' and '..'. A missing folder makes opendir() return false.

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

$handle = opendir($folder);
$entries = [];
while (($entry = readdir($handle)) !== false) {
    $entries[] = $entry;
}
closedir($handle);

sort($entries);
echo "Entries in empty folder: ", implode(', ', $entries), PHP_EOL;
rmdir($folder);

$result = @opendir(__DIR__ . '/no-such-folder');
var_dump($result);

How It Works

  1. A brand new folder is empty, but readdir() still returns the two special entries '. ' and '..'.
  2. We sort the entries so the output is predictable on every system.
  3. opendir() on a folder that does not exist returns false, and @ hides the warning for this demo.
  4. In real code, check $handle === false before calling readdir().

Example 3 — Separating Files from Sub-folders

readdir() only gives names. We combine it with is_dir() to tell sub-folders from files.

PHP
<?php
$folder = __DIR__ . '/demo-opendir-kinds';
mkdir($folder);
mkdir($folder . '/docs');
file_put_contents($folder . '/readme.txt', 'hello');

$handle = opendir($folder);
$lines = [];
while (($entry = readdir($handle)) !== false) {
    if ($entry === '.' || $entry === '..') {
        continue;
    }
    $kind = is_dir($folder . '/' . $entry) ? 'folder' : 'file';
    $lines[] = $entry . ' (' . $kind . ')';
}
closedir($handle);

sort($lines);
echo implode(PHP_EOL, $lines), PHP_EOL;

unlink($folder . '/readme.txt');
rmdir($folder . '/docs');
rmdir($folder);

How It Works

  1. The demo folder holds one sub-folder called docs and one file called readme. txt.
  2. readdir() returns only the name, so we add the folder path before calling is_dir().
  3. continue skips '. ' and '..' so only real items are listed.
  4. After sorting, each line shows the name and whether it is a folder or a file.

Frequently Asked Questions

It opens a folder for reading and returns a directory handle. You use that handle with readdir() to get the entries and with closedir() to close the folder.
It returns false and raises an E_WARNING. Check with === false, or test with is_dir() first, before using the handle.
It is good practice. PHP closes the handle at the end of the script, but closing it yourself frees the resource earlier and keeps your code tidy.
For small folders, scandir() is simpler because it returns an array. Use opendir() with readdir() when you want to process entries one at a time, for example in very large folders.
They are special entries that mean 'this folder' and 'the parent folder'. Every folder has them, so skip them in your loop if you only want real files and folders.

Next: readdir()

readdir() returns the name of the next entry in an open directory. Each call gives you one.

readdir() 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