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

phpis_dir(string $filename): bool

Example

Let's check the existence of the 'docs' directory:

php<?php
  $res = is_dir('docs');
  var_dump($res);
?>

Code execution result (if the directory exists):

phptrue

Example

Let's check the existence of the 'unknown_folder' directory:

php<?php
  $res = is_dir('unknown_folder');
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Let's check whether the specified path is a directory and not a file:

php<?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