In daily PHP file operations, it is common to need to check whether a directory exists. For example, before creating files or folders, we usually need to verify if the directory already exists to avoid duplication or errors.
PHP provides a built-in function is_dir() to determine if a directory exists. This function accepts a directory path as a parameter and returns a boolean value indicating whether the path is an existing directory.
bool is_dir(string $directory)<?php
$directory = "my_directory";
if (is_dir($directory)) {
echo "Directory \"{$directory}\" exists";
} else {
echo "Directory \"{$directory}\" does not exist";
}
?>With the is_dir() function, PHP developers can easily determine whether a path is an existing directory. This is very useful in scenarios like file uploads, log management, and cache directory creation.