PHP is_dir() Function: Description, Parameters, Return Values, and Usage Examples

The article explains PHP's is_dir() function, detailing its signature, parameter description, return values, and provides two practical code examples demonstrating how to check if a path is a directory and handle the result.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP is_dir() Function: Description, Parameters, Return Values, and Usage Examples

The PHP is_dir() function determines whether a given filename refers to an existing directory.

Signature: bool is_dir(string $filename) Parameters: $filename – the path to check; if it exists and is a directory, the function returns true, otherwise false. Relative paths are resolved against the current working directory.

Return value: true if the path is a directory, false otherwise.

Example 1:

<?php
var_dump(is_dir('a_file.txt'));
var_dump(is_dir('bogus_dir/abc'));
var_dump(is_dir('..'));
?>

Output:

bool(false)
bool(false)
bool(true)

Example 2:

<?php
$file = "images";
if (is_dir($file)) {
    echo "$file is a directory";
} else {
    echo "$file is not a directory";
}
?>

This will echo whether images is a directory.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPfunctionFilesystemis_dir
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.