Backend Development 5 min read

How to Traverse Directories and Retrieve File Information in PHP

This article explains how to use PHP functions such as opendir, readdir, is_dir, and scandir to open directories, read file names, perform recursive traversal of subfolders, and retrieve file information, providing clear code examples for each method.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Traverse Directories and Retrieve File Information in PHP

PHP is a widely used programming language, and traversing directories to obtain file information is a common task. This article demonstrates how to implement directory traversal in PHP.

opendir() Function

The opendir() function opens a directory; you must pass the directory path as a parameter. After successfully opening the directory, you can read its contents using the readdir() function.

Example code:

<code>$dir = './test';  // directory path
$handle = opendir($dir);  // open directory
while ($file = readdir($handle)) {  // loop through all files
    if ($file != "." && $file != "..") {  // exclude "." and ".."
        echo $file . "<br>";  // output file name
    }
}
closedir($handle);  // close directory</code>

In the example above, a folder named "test" is opened, and all file names inside it are printed while ignoring the special entries "." and "..".

is_dir() Function and Recursive Traversal

To traverse all subdirectories and files under a directory, a recursive approach can be used. During traversal, the is_dir() function helps determine whether the current item is a directory.

Example code:

<code>function scanDir($dir){
    if(!is_dir($dir)){
        echo '不是一个有效的文件夹路径';
        return;
    }
    $handle = opendir($dir);  // open directory
    while($file = readdir($handle)){  // loop through all files
        if($file == '.' || $file == '..'){
            continue; // skip special entries
        }
        $path = $dir . '/' . $file;  // full path of current item
        if(is_dir($path)){  // if it is a directory
            echo '<b>' . $file . '</b><br>';  // output directory name
            scanDir($path);  // recursively traverse subdirectory
        } else {  // if it is a file
            echo $file . '<br>';  // output file name
        }
    }
    closedir($handle);  // close directory
}
</code>

The scanDir() function accepts a directory path, checks whether the path is a valid folder, opens it, and iterates over its contents. When a subdirectory is encountered, the function outputs its name and calls itself recursively to explore deeper levels; files are simply printed.

scandir() Function

PHP also provides the scandir() function, which retrieves all file and subdirectory names in a directory at once. Unlike opendir() , scandir() returns an array containing the names.

Example code:

<code>$dir = './test';  // directory path
$files = scandir($dir);  // get all file and folder names
foreach($files as $file){  // loop through the array
    if($file == '.' || $file == '..'){
        continue; // skip special entries
    }
    echo $file . '<br>';  // output file or folder name
}
</code>

This example calls scandir() to obtain an array of all entries in the "test" folder, skips the special "." and ".." entries, and prints each remaining file or directory name.

In summary, the article presents three ways to traverse directories in PHP: using opendir() with readdir() , employing a recursive function with is_dir() , and leveraging the convenient scandir() function to retrieve all entries at once.

PHPreaddirscandiropendirdirectory traversalrecursive
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.