WP-Mix

A fresh mix of code snippets and tutorials

Recursively display images via PHP

Here is an easy way to display all images in a directory — including images contained in sub-directories.

Just add the following snippet to your script:

<?php

// directory
$directory = "img/*/";

// file type
$images = glob("" . $directory . "*.gif");

foreach ($images as $image) {
	echo '<img src="' . $image . '" /> ';
} 

?>

This is a basic example showing how PHP’s glob() function can be used to display any/all images from a directory in recursive fashion. For example, I adapted this technique to auto-display all images from a WordPress /uploads/ directory that had images organized by year, so recursive functionality was required to display the images from each year’s directory.

★ Pro Tip:

USP ProSAC Pro