Directory functions in PHP let a script find out where it is running, move to another folder, and read what a folder contains. They are the base for file browsers, upload managers, backup scripts, and any code that needs to walk through files.
Use getcwd() to read the current working directory and chdir() to change it. Relative paths such as data/file.txt are resolved from that directory, so know where it points before you open files.
To list a folder, call scandir() for a ready-made sorted array. For one entry at a time, use opendir(), readdir(), and closedir(), with rewinddir() to start over. The dir() function wraps the same handle in a Directory object.
Use scandir() for small folders where an array is enough. Use opendir() with readdir() when a folder may hold thousands of files and you want to process them one by one.
Skip . and .. and use !== false.
Listings include . and ... Loop with readdir($handle) !== false, not a bare truthy check, so an entry named 0 is not lost.
chroot() has hard limits.
chroot() changes the root directory of the process. It needs root privileges, does not exist on Windows, and is not available in most web server SAPIs, so treat it as a CLI-only tool. For web code, keep files under a fixed base folder and validate every path instead.
Syntax
PHP Syntax
The usual locate, change, and list pattern:
PHP
$cwd = getcwd(); // string or false
chdir(__DIR__ . '/data'); // true on success, false on failure
$names = scandir('data'); // sorted array, includes . and ..
$handle = opendir('data'); // resource handle or false
while (($entry = readdir($handle)) !== false) {
echo $entry, PHP_EOL; // one entry per call
}
rewinddir($handle); // go back to the first entry
closedir($handle); // release the handle
$d = dir('data'); // Directory object
while (false !== ($entry = $d->read())) {
echo $entry, PHP_EOL;
}
$d->close();
Reference
Core Ideas
What to remember when you work with directories in PHP:
Three short scripts that show how directories work in PHP. Each one assumes the script sits in a folder named app that has a data subfolder containing a.txt, b.txt, and notes.md.
Example 1 — Working directory
Read the current directory with getcwd(), move into a subfolder with chdir(), and go back.
dir() opens the folder and returns a Directory object, so get_class() prints Directory.
The first loop reads every entry, so the handle is at the end. Calling rewind() moves it back to the start, and the second count gives the same result.
close() releases the handle, just like closedir() does for the function style.
Continue
Next: PHP Directory Functions Index
After you know the working directory and the two listing styles, open the functions index for all 9 guides.
They are built-in functions for working with folders: read or change the current working directory, list the files and subfolders inside a directory, and open a directory handle you can read entry by entry.
scandir() reads the whole directory at once and returns a sorted array, which is short and simple for small folders. opendir(), readdir(), and closedir() read one entry at a time, which uses less memory on very large folders and gives you control over the loop. readdir() returns entries in no guaranteed order.
readdir() returns false when there are no more entries, but a file or folder named 0 is a valid entry whose value can look false in a loose comparison. Use while (($entry = readdir($handle)) !== false) so such names are not skipped.
getcwd() returns the current working directory as a string, or false on failure. chdir() changes it, which affects how relative paths such as data/file.txt are resolved for the rest of the script. It returns true on success and false on failure.
Every directory contains two special entries: . is the directory itself and .. is its parent. Skip them with array_diff() or an if check before you process real files.
Usually not. chroot() needs root privileges, is not available on Windows, and is not available in most web server SAPIs. It is mainly for the CLI, CGI, or embed SAPIs run by a privileged user. For web scripts, use fixed base directories and validate paths instead.