What are some common pitfalls when using PHP to read image folders and display them on an HTML page?

One common pitfall when using PHP to read image folders and display them on an HTML page is not checking if the file is actually an image before trying to display it. This can lead to errors if non-image files are present in the folder. To solve this issue, you can use the `getimagesize()` function to check if the file is an image before displaying it.

$dir = 'images/';
$files = scandir($dir);

foreach($files as $file) {
    $filePath = $dir . $file;
    
    if(is_file($filePath) && getimagesize($filePath)) {
        echo '<img src="' . $filePath . '" alt="' . $file . '">';
    }
}