In PHP, is_dir() tells you whether a path points to an existing directory (folder). It returns true for a folder and false for anything else.
It is the safe way to check a folder before you list it, write into it or try to create it. A path that does not exist, or that is a normal file, gives false.
Syntax
Call is_dir() with the values you need. Start with this line.
PHP
$isFolder = is_dir('uploads');
Call
What it does
is_dir('uploads')
Returns true if an uploads folder exists.
is_dir(__DIR__)
Returns true because the folder holding the current script exists.
is_dir('notes.txt')
Returns false because a file is not a directory.
is_dir() follows symbolic links, so a link that points to a folder also returns true. PHP remembers the result for a short time, so call clearstatcache() if a folder is created or removed while the script runs and you check again.
Library declarations
PHP
is_dir(string $filename): bool
Parameters
Parameter
Description
filename
Required. The path to check. It can be relative or absolute.
Return Value
Return Value
Description
bool
Returns true if the path exists and is a directory, otherwise false.
PHP Examples
Example 1 — Check That a Folder Exists
Check the temporary folder of your system, which always exists.
PHP
<?php
$dir = sys_get_temp_dir();
if (is_dir($dir)) {
echo "Yes, it is a folder", PHP_EOL;
} else {
echo "No, it is not a folder", PHP_EOL;
}
Output
Yes, it is a folder
How It Works
sys_get_temp_dir() returns the path of the system temporary folder.
is_dir() checks whether that path is a directory and returns true.
The if branch runs and prints the message.
No folder is created or changed, so the script is completely safe to run.
Example 2 — Files and Missing Paths Return False
is_dir() is only true for folders. A normal file and a path that does not exist both give false.