PHP Directory Functions

Beginner
3 min read
Updated: Nov 2026
9 function guides
Function index

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().

Beginner path.

Start with getcwd() and chdir()—then learn scandir() for quick listings and opendir() with readdir() for large folders.

Good to know.

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:

PHP
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 functions

Read or change the directory that relative paths start from. chroot() needs root privileges and is not available on Windows.

FunctionDescriptionTutorial
getcwd()Get the current working directory.Try it
chdir()Change the current working directory.Try it
chroot()Change the root directory (needs privileges; not on Windows).Try it

Directory handle

4 functions

Open a directory, read entries one at a time, rewind, and close. Loop readdir() with !== false.

FunctionDescriptionTutorial
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 functions

Shortcuts that wrap the handle for you: an object with methods, or a sorted array of names.

FunctionDescriptionTutorial
dir()Return a Directory object for reading a folder.Try it
scandir()List files and directories as a sorted array.Try it

Frequently Asked Questions

They are built-in functions for working with folders: read or change the current working directory, open a directory handle and read entries one at a time, or list a whole folder at once as an array or a Directory object.
The Directory page teaches the ideas: the working directory, scandir versus opendir and readdir, and the dir() object style. This index lists every function tutorial so you can open getcwd(), chdir(), scandir(), and the rest.
Start with getcwd() and chdir() to understand the working directory. Then learn scandir() for quick listings, and opendir(), readdir(), and closedir() when you need to read a folder entry by entry.
readdir() returns false when no entries are left, but a file named 0 is a valid entry that a loose check can mistake for false. Loop with while (($entry = readdir($handle)) !== false) so every entry is processed.
No. 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.
If you are new, read PHP Directory first. Then open getcwd(), chdir(), and scandir() from the tables below.

Start with chdir()

Open the first guide—syntax, examples, and FAQs.

chdir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful