PHP Function Reference

PHP is_dir() Function



The PHP is_dir() function checks whether the given filename is a directory.

Note: The results of this function are cached. Use clearstatcache() function to clear the cache.

Syntax

is_dir(filename)

Parameters

filename Required. Specify the path to the file to check. If filename is a relative filename, it will be checked relative to the current working directory. If filename is a symbolic or hard link then the link will be resolved and checked. If open_basedir is enabled further restrictions may apply.

Return Value

Returns true if the filename exists and is a directory, false otherwise.

Exceptions

Upon failure, an E_WARNING is thrown.

Example:

The example below shows the usage of is_dir() function.

<?php
$dir1 = 'test.txt';
$dir2 = 'temp/test';
$dir3 = '..';  //one dir up

//checking if $dir1 exists and is a directory
if(is_dir($dir1)) {
  echo "$dir1 is a directory.\n";
} else {
  echo "$dir1 is not a directory.\n";
}

//checking if $dir2 exists and is a directory
if(is_dir($dir2)) {
  echo "$dir2 is a directory.\n";
} else {
  echo "$dir2 is not a directory.\n";
}

//checking if $dir3 exists and is a directory
if(is_dir($dir3)) {
  echo "one directory up exists.\n";
} else {
  echo "one directory up do not exist.\n";
}
?>

The output of the above code will be:

test.txt is not a directory.
temp/test is not a directory.
one directory up exists.

❮ PHP Filesystem Reference