Backend Development 5 min read

Using PHP is_dir() to Check and Traverse Directories

This article explains PHP's is_dir() function, shows how it determines whether a path is a directory, provides basic and advanced code examples for checking and traversing directories, and highlights important usage considerations for reliable file system operations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP is_dir() to Check and Traverse Directories

PHP is a widely used server‑side scripting language with a rich function library. This article introduces one of its common functions, is_dir() , which is used to determine whether a given path refers to a directory.

The is_dir() function accepts a single argument – the path to be checked – and returns a boolean value indicating if the path is a directory.

Using is_dir() is practical in many scenarios, such as file‑management systems where you need to distinguish between files and folders provided by users.

Below is a simple code example:

<?php
$dir = "path/to/directory";
// Check if the path is a directory
if (is_dir($dir)) {
    echo "路径 {$dir} 是一个目录";
} else {
    echo "路径 {$dir} 不是一个目录";
}
?>

In this example, the variable $dir stores a path string. By calling is_dir($dir) , the script determines whether the path is a directory and outputs the appropriate message based on the boolean result.

Before invoking is_dir() , ensure that the path exists and is accessible; otherwise the function cannot operate correctly.

Beyond simple checks, is_dir() can be used in more complex scenarios, such as traversing the contents of a folder.

Here is an example that demonstrates how to iterate over all files and sub‑directories within a given directory:

<?php
$dir = "path/to/directory";
// Check if the path is a directory
if (is_dir($dir)) {
    // Open the directory
    if ($dh = opendir($dir)) {
        // Loop through directory entries
        while (($file = readdir($dh)) !== false) {
            // Skip '.' and '..'
            if ($file == "." || $file == "..") {
                continue;
            }
            // Build full path for the entry
            $path = $dir . '/' . $file;
            // Determine if entry is a directory or a file
            if (is_dir($path)) {
                echo "{$path} 是一个目录";
            } else {
                echo "{$path} 是一个文件";
            }
        }
        // Close the directory
        closedir($dh);
    }
} else {
    echo "路径 {$dir} 不是一个目录";
}
?>

This script first checks whether the given path is a directory, then uses opendir() to open it, and iterates through its entries with readdir() , skipping the special entries "." and "..". For each entry, it builds the full path and calls is_dir() again to decide if it is a sub‑directory or a file, outputting the result accordingly. Finally, closedir() closes the directory handle.

In summary, the article covered the PHP is_dir() function, provided concrete code examples for both simple directory checks and recursive traversal, and explained key considerations such as ensuring path existence and permissions. The function is a valuable tool for developers needing precise file system operations.

backend developmentPHPFile Systemis_dirdirectory traversal
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

login 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.