How can you filter out non-image files when processing a directory of files in PHP?

When processing a directory of files in PHP, you can filter out non-image files by checking the file extensions against a list of commonly used image file extensions such as jpg, jpeg, png, gif, etc. This can be done using the pathinfo() function to extract the file extension and then comparing it against a predefined list of image extensions.

$directory = "/path/to/directory/";
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');

$files = scandir($directory);

foreach($files as $file) {
    $extension = pathinfo($file, PATHINFO_EXTENSION);
    if(in_array(strtolower($extension), $allowedExtensions)) {
        // Process image file
        echo "Processing image file: $file\n";
    }
}