Master PHP’s is_dir() Function: Quick Guide & Real-World Examples

Learn how PHP’s is_dir() function checks whether a given path is a directory, explore practical use cases such as file management, and follow step‑by‑step code examples that demonstrate simple checks, directory traversal, and proper handling of permissions.

php Courses
php Courses
php Courses
Master PHP’s is_dir() Function: Quick Guide & Real-World Examples

PHP is a popular server‑side scripting language with a rich function library. This article introduces the commonly used is_dir() function, its purpose, and practical scenarios.

The is_dir() function determines whether a given path is a directory, accepting a single path argument and returning a boolean.

Using is_dir() is useful in many situations, such as distinguishing files from folders in a file‑management system.

Example code:

<?php
$dir = "path/to/directory";

// Check if the path is a directory
if (is_dir($dir)) {
    echo "Path {$dir} is a directory";
} else {
    echo "Path {$dir} is not a directory";
}
?>

In the example, $dir stores a path string. Calling is_dir() on it returns true for a directory and false otherwise.

Before calling is_dir(), ensure the path exists and is accessible; otherwise the function will fail.

Beyond simple checks, is_dir() can be used for more complex tasks like traversing a folder’s contents. The following code demonstrates iterating over all files and sub‑directories:

<?php
$dir = "path/to/directory";

// Check if the path is a directory
if (is_dir($dir)) {
    // Open the directory
    if ($dh = opendir($dir)) {
        // Read entries
        while (($file = readdir($dh)) !== false) {
            // Skip . and ..
            if ($file == "." || $file == "..") {
                continue;
            }
            $path = $dir . '/' . $file;
            // Determine if entry is a directory
            if (is_dir($path)) {
                echo "{$path} is a directory";
            } else {
                echo "{$path} is a file";
            }
        }
        // Close the directory
        closedir($dh);
    }
} else {
    echo "Path {$dir} is not a directory";
}
?>

This script first checks the path, opens it with opendir(), reads entries using readdir() while skipping “.” and “..”, builds each entry’s full path, uses is_dir() to differentiate files from directories, and finally closes the handle with closedir().

Summary

The article explains PHP’s is_dir() function, provides clear code examples for simple directory checks and recursive traversal, and highlights important considerations such as path existence and permissions, making it a valuable tool for PHP developers.

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.

BackendPHPfile managementis_dirdirectory check
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.