Image Gallery from a Directory using PHP

The PHP code will iterate though all the images in a folder and list them on the webpage as image gallery. The the sky is the limit, you can use it like batch processing code to make thumbnails or store them in the database. Just drop this PHP snippet within the body of your HTML code, using some CSS and Javascript it can create nice looking image gallery.
PHP
12345678910111213141516171819
<?php 
$folder_path = 'images/'; //image folder path

$folder = opendir($folder_path); 

 while (false !== ($entry = readdir($folder))) {
	if ($entry != "." &#038;&#038; $entry != ".." &#038;&#038; $entry != "Thumb.db") {
		
		$file_path = $folder_path.$entry;
		$ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
		if($ext=='jpg' || $ext =='png' || $ext == 'gif')
		{
			echo '<img src="'.$file_path.'" />';
		}
	}
}

closedir($folder );
?>
Here’s another one using PHP glob(). But it can be bit slower than above example, because it scans sub directories and parses the arguments.
PHP
123456789
<?php // display source code
$folder_path = 'images/';

    $files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);

    foreach($files as $file){
       echo '<img src="'.$file.'" />';
    }
?>