opendir()
opendir() — Open 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
Opens up a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.
Signature
opendir(string $directory, ?resource $context = null): resource|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $directory | string | The directory path to open. |
| $context | ?resource | For a description of the context parameter, refer to the streams section of the manual. |
Return value
Returns a directory handle on success, or false on failure
Example
A runnable snippet:
if ($handle = opendir('/path/to/files')) { echo "Entries:\n"; /* Correctly handling directory entries that may be considered falsy */ while (false !== ($entry = readdir($handle))) { if ($entry === '.' || $entry === '..') { continue; } echo "$entry\n"; } closedir($handle); }
Output:
Entries: base en fr output.md test.php
Changelog
| Version | Change |
|---|---|
| 8.0.0 | context is now nullable. |
See also
readdir(), rewinddir(), closedir(), dir(), is_dir(), glob(), scandir()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page opendir() (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.