The is_dir Function
The is_dir
function checks whether the specified path exists and whether it is a directory.
The function returns true
if the path exists and is a directory, and false
otherwise.
A string value is passed as a parameter - the path to the directory being checked.
Syntax
is_dir(string $filename): bool
Example
Let's check the existence of the 'docs'
directory:
<?php
$res = is_dir('docs');
var_dump($res);
?>
Code execution result (if the directory exists):
true
Example
Let's check the existence of the 'unknown_folder'
directory:
<?php
$res = is_dir('unknown_folder');
var_dump($res);
?>
Code execution result:
false
Example
Let's check whether the specified path is a directory and not a file:
<?php
$path = 'test.txt';
if (is_dir($path)) {
echo "'$path' is a directory";
} else {
echo "'$path' is NOT a directory";
}
?>
See Also
-
the
file_exists
function,
which checks for the existence of a file/directory -
the
is_file
function,
which checks a file -
the
scandir
function,
which reads the contents of a directory