dir()

dir() — Return an instance of the Directory class.

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

A pseudo-object-oriented mechanism for reading a directory. The given directory is opened.

Signature

dir(string $directory, ?resource $context = null): Directory|false

Parameters

ParameterTypeWhat it takes
$directorystringDirectory to open
$context?resourceA context stream resource.

Return value

Returns an instance of Directory, or false in case of error.

Example

A runnable snippet:

$d = dir("/etc/php5"); echo "Handle: " . $d->handle . "\n"; echo "Path: " . $d->path . "\n"; while (false !== ($entry = $d->read())) { echo $entry."\n"; } $d->close();

Output:

Handle: Resource id #2 Path: /etc/php5 . .. apache cgi cli

Changelog

VersionChange
8.0.0context is now nullable.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page dir() (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.