How can readdir be used to display images in a table format in PHP?

To display images in a table format using readdir in PHP, you can first read the files in a directory using readdir, filter out only the image files, and then display them in a table format using HTML. This can be achieved by looping through the files, checking if they are images using functions like getimagesize, and then displaying them in a table format with appropriate HTML tags.

<?php
$dir = "images/";
$files = scandir($dir);

echo "<table>";
foreach($files as $file){
    if(is_file($dir . $file) && in_array(pathinfo($file, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif'])){
        echo "<tr><td><img src='".$dir.$file."' width='100'></td></tr>";
    }
}
echo "</table>";
?>