PHP递归遍历指定目录的所有文件并统计文件数量

1. Introduction

When working with files and directories in PHP, at some point, you might need to recursively traverse a directory and all its subdirectories to perform a certain action, such as printing the name and size of all files, filtering files based on their extension, or computing the total size of a directory tree. In this tutorial, we’ll explore how to use PHP to recursively traverse a directory and count the number of files it contains.

2. Using RecursiveDirectoryIterator and RecursiveIteratorIterator

One of the best ways to traverse a directory recursively in PHP is by using the RecursiveDirectoryIterator class and its companion class, the RecursiveIteratorIterator class. The RecursiveDirectoryIterator class is used to iterate over a directory tree in a recursive way and the RecursiveIteratorIterator class is used to iterate over the objects returned by the RecursiveDirectoryIterator class in a flat way, i.e., as if they were returned by a regular Iterator interface.

To use these classes, we first create an instance of the RecursiveDirectoryIterator class and pass it the path of the directory we want to traverse, as shown below:

$dir = new RecursiveDirectoryIterator('/path/to/directory');

Next, we create an instance of the RecursiveIteratorIterator class and pass it the instance of the RecursiveDirectoryIterator we just created, as shown below:

$iterator = new RecursiveIteratorIterator($dir);

Now we can iterate over the contents of the directory and its subdirectories using a foreach loop. Each iteration returns an instance of the SplFileInfo class, which represents a file or directory in the directory tree. We can use the isFile() method to check if the current item is a file and then increment a counter if it is. Here’s the complete code:

3. Counting Files in a Directory

3.1. RecursiveDirectoryIterator and RecursiveIteratorIterator Example

$dir = new RecursiveDirectoryIterator('/path/to/directory');

$iterator = new RecursiveIteratorIterator($dir);

$count = 0;

foreach ($iterator as $file) {

if ($file->isFile()) {

$count++;

}

}

echo "The directory contains $count files.";

In the above example, we’ve used the $count variable to keep track of the number of files and incremented it within the loop whenever a file is detected. Finally, we print the total number of files using the echo statement.

3.2. Counting Files by Extension

Sometimes, we might want to count files based on their extension. For example, we might want to count how many files with the .txt extension there are in the directory structure. We can achieve this by modifying the code we wrote in the previous section to include a check for the extension of each file using the getExtension() method of the SplFileInfo class.

$dir = new RecursiveDirectoryIterator('/path/to/directory');

$iterator = new RecursiveIteratorIterator($dir);

$count = 0;

foreach ($iterator as $file) {

if ($file->isFile() && $file->getExtension() === 'txt') {

$count++;

}

}

echo "The directory contains $count txt files.";

In the above code, we’ve added a check within the foreach loop to only increment the counter if the current item is a file and has a .txt extension. We can change the extension to any other extension we want to count.

4. Conclusion

In this tutorial, we’ve explored how to use PHP to recursively traverse a directory and count the number of files it contains. We’ve shown how to use the RecursiveDirectoryIterator class and its companion class, the RecursiveIteratorIterator class, to iterate over the contents of a directory and its subdirectories. We’ve also shown how to count files by extension, by adding a simple check to the foreach loop. We hope you found this tutorial helpful!

后端开发标签