PHP is_dir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
filenameRequired. The path to check. It can be relative or absolute.

Return Value

Return ValueDescription
boolReturns 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;
}

How It Works

  1. sys_get_temp_dir() returns the path of the system temporary folder.
  2. is_dir() checks whether that path is a directory and returns true.
  3. The if branch runs and prints the message.
  4. 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.

PHP
<?php
$file = sys_get_temp_dir() . '/is_dir_edge.txt';
file_put_contents($file, 'x');

echo "File: ", is_dir($file) ? 'yes' : 'no', PHP_EOL;
echo "Missing path: ", is_dir($file . '.missing') ? 'yes' : 'no', PHP_EOL;
echo "Temp folder: ", is_dir(sys_get_temp_dir()) ? 'yes' : 'no', PHP_EOL;

unlink($file);

How It Works

  1. A small text file is created so there is a real file to test.
  2. is_dir() on a file returns false because a file is not a directory.
  3. A path that does not exist also returns false and never raises a warning.
  4. Only the temp folder gives true.

Example 3 — Create a Folder Only If It Is Missing

A common pattern is to check with is_dir() and call mkdir() when the folder does not exist yet. The loop runs the check twice to show both cases.

PHP
<?php
$dir = sys_get_temp_dir() . '/is_dir_uploads';

for ($i = 1; $i <= 2; $i++) {
    if (!is_dir($dir)) {
        mkdir($dir);
        echo "Created folder", PHP_EOL;
    } else {
        echo "Folder already exists", PHP_EOL;
    }
}

rmdir($dir);

How It Works

  1. On the first pass the folder does not exist, so !is_dir() is true and mkdir() creates it.
  2. On the second pass is_dir() returns true, so the else branch runs.
  3. This check avoids the warning that mkdir() gives when the folder is already there.
  4. rmdir() removes the demo folder at the end.

Frequently Asked Questions

It checks whether a path is an existing directory and returns true or false.
file_exists() is true for both files and folders. is_dir() is true only for folders.
Yes. If the link points to a directory, is_dir() returns true. Use is_link() to find out whether the path is a link.
The path may be wrong, relative to a different working folder, or PHP may not have permission to read it. Try an absolute path built with __DIR__.
Use if (!is_dir($dir)) { mkdir($dir, 0755, true); }. The true argument also creates any missing parent folders.

Next: is_executable()

is_executable() tells you whether a file exists and can be run as a program. It returns tr.

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

8 people found this page helpful