PHP Directory Functions

About This Index
This page is the PHP Directory Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn how the working directory and the two listing styles fit together on /php/directory. Use this page when you need a specific function such as getcwd(), scandir(), or readdir().
Start with getcwd() and chdir()—then learn scandir() for quick listings and opendir() with readdir() for large folders.
chroot() needs root privileges and is not available on Windows or in most web server SAPIs. Loop readdir() with !== false so an entry named 0 is not skipped.
How to Call a PHP Directory Function
Directory helpers are global functions—call them directly. dir() returns a Directory object whose methods you call afterward:
echo getcwd(); // current working directory
chdir('data'); // change into a subfolder
print_r(scandir('.')); // sorted array of names
$handle = opendir('.');
while (($entry = readdir($handle)) !== false) {
echo $entry, PHP_EOL; // use !== false, not a truthy check
}
rewinddir($handle);
closedir($handle);
$d = dir('.'); // object style
echo $d->read();
$d->close(); PHP Directory Functions Table
All function guides in this PHP tutorial series (9 tutorials in 3 categories).
Working directory
3 functionsRead or change the directory that relative paths start from. chroot() needs root privileges and is not available on Windows.
Directory handle
4 functionsOpen a directory, read entries one at a time, rewind, and close. Loop readdir() with !== false.
| Function | Description | Tutorial |
|---|---|---|
opendir() | Open a directory handle. | Try it |
readdir() | Read the next entry from a directory handle. | Try it |
rewinddir() | Rewind the directory handle to the first entry. | Try it |
closedir() | Close a directory handle. | Try it |
Convenience
2 functionsShortcuts that wrap the handle for you: an object with methods, or a sorted array of names.
Frequently Asked Questions
Start with chdir()
Open the first guide—syntax, examples, and FAQs.
6 people found this page helpful
