PHP

How to Make Simple Photo Gallery in PHP


In this article, you will learn how to make a simple photo gallery in PHP. 

Simple Photo Gallery

In order to make a simple photo gallery, you can use the glob() method, GLOB_BRACE flag and basename() method.

<?php
	// Path to folder that contains images
	$path = "C:/xampp/htdocs/gallery/";

	// Get files that ends with either jpg, jpeg, png from $path
	$images = glob("$path*.{jpg,jpeg,png}", GLOB_BRACE);
 
	// Display images in a photo gallery
	foreach ($images as $i) {
  		printf("<img src='gallery/%s'/>", basename($i));
	}
?>

Result

Note: The glob() method functions by searching for path names that match the supplied pattern. The GLOB_BRACE flag functions by expanding options in the curly braces. In this example, GLOB_BRACE looks for a path that ends with either jpg, jpeg, or png. The basename() method functions by returning the file name from the supplied path.


Share on social media

//