PHP Directory is_dir() Function (With Examples)

In this tutorial, we will learn about the PHP is_dir() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP is_dir() function

The full form of is_dir is "Is Directory", the function is_dir() is used to check whether a file system is a directory or whether a directory exists or not.

Syntax

The syntax of the is_dir() function:

is_dir(directory);

Parameters

The parameters of the is_dir() function:

  • directory – Directory name (or path) to be checked whether it exists or not.

Return Value

It returns a Boolean value, "TRUE" (1) – if directory exists or "FALSE" (null) – if the directory does not exist.

PHP is_dir() Function Example

PHP code to check whether a directory exists or not.

<?php
//creating directories
mkdir("/home/folder1");
mkdir("/home/folder2");
mkdir("/home/folder3");

//checking directories are exist or not
echo is_dir("/home/folder1") . "<br/>";
echo is_dir("/home/folder2") . "<br/>";
echo is_dir("/home/folder3") . "<br/>";
echo is_dir("/home/folder4") . "<br/>";

//checking through the conditions
if (is_dir("/home/folder1")) {
    echo "/home/folder1 exists" . "<br/>";
} else {
    echo "/home/folder1 does not exist" . "<br/>";
}

if (is_dir("/home/folder2")) {
    echo "/home/folder2 exists" . "<br/>";
} else {
    echo "/home/folder2 does not exist" . "<br/>";
}

if (is_dir("/home/folder3")) {
    echo "/home/folder3 exists" . "<br/>";
} else {
    echo "/home/folder3 does not exist" . "<br/>";
}

if (is_dir("/home/folder4")) {
    echo "/home/folder4 exists" . "<br/>";
} else {
    echo "/home/folder4 does not exist" . "<br/>";
}
?>

Output

The output of the above example is:

1
1
1

/home/folder1 exists
/home/folder2 exists
/home/folder3 exists
/home/folder4 does not exist

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.