PHP list images in a directory

Some cases like image in image gallery, you need to list all the images from directory and display in browser. In this article am going to explain you how to list all images from a directory using PHP. We are going to use PHP inbuilt function like globe() to fetch all images from a folder.

PHP list images in a directory

You can view demo and download how to list all Images from a directory using PHP:

Live Demo Download

PHP get images from Folder

Lets look at the PHP show images from folder script

//Path to folder which contains images
$dirname = "images/";

//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."*");

//Display image using foreach loop
foreach($images as $image){
//print the image to browser with anchor tag (Use if you want really :) )
echo '<a href="'.$image.'" target="_blank"><img src="'.$image.'" height="100" width="100" /></a>';

Now save the above file and run. PHP will look inside the folder and fetch all the images and display in browser.If your folder contains not only images some other files also means, we need to put an array of accepted file formats.

Hope that this will help you. Don’t forget to make the free email subscription for more scripts and tutorials from us.

1 Comment PHP list images in a directory

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>