Web Development & WordPress

Displaying All Images from a Folder Using PHP

In this script, I demonstrate how to retrieve and display all JPG and PNG images from a specified folder using PHP. The code uses the glob function to fetch images with specific extensions and then displays the images using HTML.

<?php
$img = glob("images/*.jpg");
echo "Showing jpg images from images folder:<br>";
echo "<pre>";
print_r($img);
echo "</pre>";

$img = glob("images/*.png");
echo "Showing png images from images folder:<br>";
echo "<pre>";
print_r($img);
echo "</pre>";

$img = glob("images/*.{jpg,png}", GLOB_BRACE);
echo "Showing all  images from images folder:<br>";
echo "<pre>";
print_r($img);
echo "</pre>";

echo "Showing the images using html from images folder<br>";
foreach ($img as $image) {
    echo "<img src={$image} width=100px /><br>";
}
?>

OUTPUT

Leave a comment