How can you count the number of .jpg files in a specific folder with arbitrary names in PHP?

To count the number of .jpg files in a specific folder with arbitrary names in PHP, you can use the glob() function to retrieve an array of file names matching a specified pattern. You can then loop through this array and check if each file has a .jpg extension. Increment a counter variable for each file that matches the criteria to get the total count.

$folderPath = 'path/to/folder';
$jpgFiles = glob($folderPath . '/*.jpg');
$jpgCount = 0;

foreach ($jpgFiles as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) == 'jpg') {
        $jpgCount++;
    }
}

echo "Number of .jpg files in the folder: " . $jpgCount;