readdir()

readdir() — Read entry from directory handle.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.

Signature

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

Parameters

ParameterTypeWhat it takes
$dir_handle?resourceA directory handle resource previously opened with opendir(). If dir_handle is null the last handle opened using opendir() will be used.

Return value

Returns the entry name on success, or false on failure. This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Example

The PHP manual ships no example for readdir(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.5.0Using null for dir_handle is now deprecated. Instead, the last opened directory handle should be explicitly provided.
8.0.0dir_handle is now nullable.

See also

opendir(), rewinddir(), closedir(), dir(), is_dir(), glob(), scandir()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page readdir() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.