Home / Web Development / PHP / How to display existing image from specified folder/directory – PHP

How to display existing image from specified folder/directory – PHP

Hi all,
Today I’m going to share important topic based on “how to display existing image from corresponding directory in php”. Nobody try to discuss how to display existing content in web page.
Everybody knows that how to upload image or something to specified directory or folder using PHP. But nobody try to display the existing content in web page. This concept is impotant in upload the profile pic or some other functionality.



We can describe this problem like 1)I have php code its perfectly working. We can upload the image and can save the image to corresponding directory and also the image can display temporary but can’t permanently. How to solve this problem.
For solving this problem, I specify two method:

I have php code which perfectly woks its upload the image and save the image to directory but the image is display temporary ! i want to display it permanently … and whenever i want to change the image it will change the image and display uploaded image




Here I explain how to display the image from a specified directory or folder using php. I want to display all the images(includes both png,jpeg and all image extension) from a directory.
In my case my directory is ‘team_member_image/’. The glob is used for this operations. The glob() function returns an array of filenames or directories matching a specified pattern.

Syntax-glob() Function

glob(pattern,flags)



Method1:Using for loop

Description:Fetch an array of filename or directories matching a specified pattern by using glob() function. The above code shows how to use the glob() function in our needs.
First fetch all the files from “team_member_image” directory. The “.” helps to fetch all the files. Fetch all the files from “team_member_image” and store it to variablename as file.
Next count the filename using count($file) and using for-loop for display all the files from a specified folder like “team_member_image”.

Method2 : Using foreach




Description: Fetch the array of filename using glob function same as first method. In method1 we use for loop but her we use foreach method, and display the images using <img> tag.

From the above two method, it will display all the contents in webpage. But if you want to display the specified extension like png, jpg etc.
Now using following way to display the content in php

Method 1: Using foreach




Display all the png image from “team_member_image” directory. We can use all extension file. We just replace the png with our extension like jpg,pdf etc.

Method2:Using for loop




Display all the png image from “team_member_image” directory




If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Loading Facebook Comments ...
Social Media Auto Publish Powered By : XYZScripts.com