Simple PHP Gallery

Anyone who has built and maintained a simple HTML-based image gallery will know how much work is involved in adding new images from time to time.

Showing Images with PHP – scandir

The following PHP code will allow for simple inclusion of the contents of an image folder, using nothing more than an FTP (File Transfer Protocol) application such as FileZilla. All you need to do is build the page with the the example code and every time you wish to add an image, you drag and drop it onto your server via FTP. You may need to modify the ‘output’ of the images to suit your requirements.

The ‘/// notes’ included within the PHP explain how it works.

<?php
$inc_array = array("jpg", "jpeg", "gif", "png", "bmp"); /// BUILD INCLUSION ARRAY
$img_dir = "./img/"; /// LOCATION OF IMAGE FOLDER
$imgs = scandir($img_dir); /// PUT FILES FROM '$img_dir' INTO AN ARRAY
foreach($imgs as $img_x) /// LOOP THROUGH ALL IMGS
{
 $ext = substr($img_x, strrpos($img_x, '.') + 1); /// EXTRACT FILE EXTENTION
 $ext = strtolower($ext); /// TO LOWER CASE
  if(in_array($ext, $inc_array)) /// IF EXTENTION IN INCLUSION ARRAY
  {
  /// Output
   echo "<img src='./img/$img_x'>"; /// DISPLAY IMAGE
   echo "<br><i>$img_x</i><br>"; /// DISPLAY IMG NAME
  ///
  }
}     
?>