Are there any best practices or recommendations for efficiently counting images in external directories using PHP scripts?

When counting images in external directories using PHP scripts, it is important to efficiently iterate through the files in the directory and only count the files that are valid images. One way to achieve this is by using the glob() function to get an array of file paths and then filter out non-image files using functions like pathinfo() or getimagesize().

$directory = 'path/to/external/directory';
$imagesCount = 0;

$files = glob($directory . '/*');
foreach ($files as $file) {
    if (exif_imagetype($file)) { // Check if the file is a valid image
        $imagesCount++;
    }
}

echo "Total number of images in directory: " . $imagesCount;