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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
<?php $folder_path = 'images/'; //image folder path $folder = opendir($folder_path); while (false !== ($entry = readdir($folder))) { if ($entry != "." && $entry != ".." && $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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
<?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.'" />'; } ?>
New question is currently disabled!