Search code examples
phpimagedirectorytabular

Display images from folder in table using php,html?


I want to display my images in table form. Here is my code:

<?php
$files = glob("images/*.*");
for ($i = 0; $i < count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
    );

    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        echo basename($image);
echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';
} else {
continue;
}
}
?>

enter image description here

Something like that picture.


Solution

  • If you want it in table the simply add table tag with and to it

    <table>
    <tr>
      <th>Image Name</th>
      <th>Image</th>
    </tr>
    <?php
    $files = glob("images/*.*");
    for ($i = 0; $i < count($files); $i++) {
        $image = $files[$i];
        $supported_file = array(
            'gif',
            'jpg',
            'jpeg',
            'png'
        );
    
        $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
        if (in_array($ext, $supported_file)) {
          echo "<tr><td>";
          echo basename($image);
          echo "</td><td>";
          echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';
          echo "</td></tr>";
    } else {
    continue;
    }
    }
    ?>
    </table>