PHP readdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, readdir() returns the name of the next entry in an open directory. Each call gives you one name, and it returns false when there are no more entries.

You use readdir() together with opendir() and closedir(). Put it in a while loop to visit every item in a folder. The names come back in the order the file system stores them, which is not alphabetical, and the list includes the special entries '.' and '..'.

Syntax

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

PHP
while (($entry = readdir($handle)) !== false) { ... }
CallWhat it does
readdir($handle)Returns the next entry name, such as 'photo.jpg', or false at the end of the folder.
($entry = readdir($handle)) !== falseThe safe loop condition. It stops only when readdir() returns false.
readdir()Reads from the last opened handle. This is deprecated in PHP 8.5 and later, so pass the handle instead.

Always compare with !== false. A file or folder can be named 0, and the string '0' counts as false in a loose check, which would end your loop too early.

Library declarations
PHP
readdir(?resource $dir_handle = null): string|false

Parameters

ParameterDescription
dir_handleOptional. The directory handle returned by opendir(). If it is left out or null, PHP uses the last opened handle. Passing null is deprecated in PHP 8.5 and later, so always pass the handle.

Return Value

Return ValueDescription
string|falseReturns the name of the next entry (just the name, not the full path). Returns false when there are no more entries.

PHP Examples

Example 1 — Looping Through a Folder

The standard readdir() loop: read one name at a time until false, skip the dot entries, and sort the result.

PHP
<?php
$folder = __DIR__ . '/demo-readdir';
mkdir($folder);
file_put_contents($folder . '/a.txt', '1');
file_put_contents($folder . '/b.txt', '2');
file_put_contents($folder . '/c.txt', '3');

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

sort($names);
echo "Found ", count($names), " files: ", implode(', ', $names), PHP_EOL;

unlink($folder . '/a.txt');
unlink($folder . '/b.txt');
unlink($folder . '/c.txt');
rmdir($folder);

How It Works

  1. opendir() gives us a handle, and each readdir($handle) call moves to the next entry.
  2. The loop condition assigns the entry and compares it with false in one step.
  3. We skip '. ' and '..' and collect the real names in an array.
  4. sort() puts the names in alphabetical order, because readdir() does not.

Example 2 — Why You Must Use !== false

A file called 0 is perfectly legal. Here is why a loose comparison is dangerous, and how the correct loop still finds the file.

PHP
<?php
var_dump((bool) '0');

$folder = __DIR__ . '/demo-readdir-zero';
mkdir($folder);
file_put_contents($folder . '/0', 'zero');
file_put_contents($folder . '/a.txt', 'a');

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

sort($names);
echo "Found: ", implode(', ', $names), PHP_EOL;

unlink($folder . '/0');
unlink($folder . '/a.txt');
rmdir($folder);

How It Works

  1. The string '0' becomes false when PHP treats it as a boolean, as the first var_dump() shows.
  2. A loop written as while ($entry = readdir($handle)) would stop as soon as it reads the file named 0.
  3. Our loop uses !== false, which only stops when readdir() really returns the boolean false.
  4. So both entries, 0 and a. txt, are found, no matter what order the file system returns them in.

Example 3 — readdir() at the End of a Folder

After the last entry has been read, readdir() returns false. We call it once more after the loop to prove it.

PHP
<?php
$folder = __DIR__ . '/demo-readdir-end';
mkdir($folder);
file_put_contents($folder . '/only.txt', 'x');

$handle = opendir($folder);
$count = 0;
while (($entry = readdir($handle)) !== false) {
    if ($entry !== '.' && $entry !== '..') {
        $count++;
    }
}
echo "Files: ", $count, PHP_EOL;

var_dump(readdir($handle));
closedir($handle);

unlink($folder . '/only.txt');
rmdir($folder);

How It Works

  1. The folder holds a single file, so after skipping the dot entries the counter ends at 1.
  2. The loop only ends because readdir() has returned false.
  3. Calling readdir() one more time still returns false, because there is nothing left to read.
  4. To read the folder again from the start, call rewinddir() or open a new handle.

Frequently Asked Questions

It returns the name of the next entry in a directory that was opened with opendir(). Calling it repeatedly walks through the whole folder, and it returns false when the end is reached.
Because an entry can be named 0, and the string '0' is treated as false in a loose comparison. With !== false, the loop only stops at the real end of the folder.
They are special entries present in every folder. '.' means the folder itself and '..' means its parent. Skip them if you only want real files and folders.
No. The order depends on the file system and can change between systems. Collect the names in an array and sort() them, or use scandir(), which sorts for you.
PHP will use the last opened directory, but this is deprecated in PHP 8.5 and later. Always pass the handle returned by opendir().

Next: rewinddir()

rewinddir() resets an open directory handle back to the start of the folder. The next read.

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