How can you read multiple directories and count the total number of files in PHP?

To read multiple directories and count the total number of files in PHP, you can use a combination of functions like scandir() to list the files in each directory and count() to count the number of files. You can loop through each directory, list the files, and increment a counter for each file found.

<?php
$directories = ['directory1', 'directory2', 'directory3'];
$totalFiles = 0;

foreach ($directories as $dir) {
    $files = scandir($dir);
    $totalFiles += count($files) - 2; // Subtract 2 to exclude '.' and '..' entries
}

echo "Total number of files in all directories: " . $totalFiles;
?>