How can we filter out only jpg files when reading files from a directory in PHP?

To filter out only jpg files when reading files from a directory in PHP, we can use the glob() function with a wildcard pattern to match only files with a .jpg extension. This function returns an array of file names that match the specified pattern.

$files = glob('path/to/directory/*.jpg');
foreach($files as $file) {
    echo $file . "<br>";
}