is_dir()
The is_dir() function is a built-in PHP function that checks whether a given path is a directory. This function returns true if the path is a directory and
What is the is_dir() Function?
The is_dir() function is a built-in PHP function that tells you whether a given path exists and points to a directory (folder). It returns true when the path is an existing directory, and false for everything else — a regular file, a path that does not exist, or a path you do not have permission to read.
It is one of PHP's filesystem "type-check" functions, alongside is_file(), is_link(), and file_exists(). Use is_dir() whenever you need to confirm a directory is there before reading it, writing into it, or creating it.
Syntax
is_dir(string $filename): bool$filename— the path to test. It can be absolute (/var/www/uploads) or relative to the script's current working directory (uploads). A trailing slash is allowed.- Return value —
trueif$filenameexists and is a directory, otherwisefalse.
Basic Example
__DIR__ is a magic constant that always resolves to an existing directory, so this example reliably prints the "is a directory" branch.
Telling a Directory From a File
The most common use of is_dir() is to branch on what a path actually is. Compare it with is_file():
<?php
$paths = [__DIR__, __FILE__, '/path/that/does/not/exist'];
foreach ($paths as $path) {
if (is_dir($path)) {
echo "$path -> directory\n";
} elseif (is_file($path)) {
echo "$path -> file\n";
} else {
echo "$path -> missing\n";
}
}__FILE__ is the full path of the current script, so it is reported as a file, while the made-up path is reported as missing.
A Practical Use: Create the Directory Only If It Is Missing
Guarding mkdir() with is_dir() avoids a warning when the folder already exists:
<?php
$dir = sys_get_temp_dir() . '/my_app_cache';
if (!is_dir($dir)) {
mkdir($dir, 0755, true); // true = create parent dirs too
echo "Created: $dir";
} else {
echo "Already exists: $dir";
}Common Gotchas
is_dir()returnsfalse, not an error, when the path does not exist. Afalseresult does not mean "this is a file" — it could simply be missing. Usefile_exists()if you only care that something is there.- Symbolic links are followed. If the path is a symlink that points at a directory,
is_dir()returnstrue. To detect the link itself, useis_link(). - Results are cached. PHP caches stat() results, so if a directory is created or deleted by another process mid-script you may see a stale answer. Call
clearstatcache()to force a fresh check. - Relative paths depend on the working directory.
is_dir('data')is resolved against the current working directory (getcwd()), not the script's location. Use__DIR__ . '/data'when you mean "next to this script."
Related Functions
| Function | Checks for |
|---|---|
is_file() | A regular file |
is_link() | A symbolic link |
file_exists() | A file or directory existing |
is_readable() | A readable path |
scandir() | Listing a directory's contents |
mkdir() | Creating a directory |
Conclusion
is_dir() is the safe, standard way to confirm a path is an existing directory before you act on it. Remember that it follows symlinks, caches its result, and resolves relative paths against the current working directory — pairing it with file_exists(), is_file(), and mkdir() covers nearly every directory-handling situation you will meet.