PHP- List All Files In A Directory

Image frame
PHP- List All Files In A Directory
Maruf
Nov 2nd, 2008
Maruf scribbled this post.

I’m working on a website for a client at the moment, and one of the features I need to do is display a bunch of images in a particular directory.

It’s actually a very simple process in PHP and only requires a small amount of code. I’ve seen a lot of examples on other forums/websites that use a ridiculous amount of code to get the same result, and i’m not quite sure why.

Here’s how to list all files in a directory

1
2
3
4
5
6
7
8
9
10
11
//path to directory to scan
$directory = "../images/team/harry/";
 
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
 
//print each file name
foreach($images as $image)
{
echo $image;
}

It’s as easy as that.

Notes

The .jpg extension can be changed to any extension. So if you want to pull out only .txt files, you just need to replace the .jpg with .txt. If you want to list ALL files, just remove the condition.


Filed away: PHP

comments

Image frame
1

Thanks that example helped me a lot!

Conor
Feb 19th, 2009
Image frame
2

Simple and powerful. Thanks, it has helped me too!

cstulo
Mar 7th, 2009
Image frame
3

Very helpful post… Thanks.

Mehedi Hasan

Mehedi Hasan
Aug 5th, 2009
Image frame
4

One of the best posts I’ve seen for some time, thanks a lot.

Ralph
Aug 27th, 2009
Image frame
5

Great code, to get better support use

$images = glob(“” . $directory . “{*.jpg,*.gif,*.png}”, GLOB_BRACE);

Thanks!

Rickard Sogge
Aug 27th, 2009
Image frame
6

Code at its best – short, sweet and easy to understand!

Thanks

James
Oct 12th, 2009
Image frame
7

How about listing files by date modified?

Matt
Jan 23rd, 2010
Image frame
8

It solved my problem.

Thank you.

Is it possible to get the absolute path of a file which is located in any location of the system using php.

satheesh
Mar 2nd, 2010
Image frame
9

So Helpful. Thanks a Lot. It helped me to show a list of news in flat files.

Victor Campusano
Mar 30th, 2010
Image frame
10

Hi mate, great script.. thank you! Just wondering, how would I go about displaying the images in order? For example if they were named 1.jpg – 10.jpg?

Thanks

Matt

Matt
Jul 9th, 2010
Image frame
11

hey everyone, here’s a function to output certain image types, with the option to put its name under the image. -sam

function list_images($folder,$type,$listnames){

$ext = ($type === ‘all’) ? ‘{*.jpg,*.gif,*.png}’ : ‘*.’.$type;
$files = glob($folder.$ext, GLOB_BRACE);
sort($files);

foreach ($files as $file) {
if ($file “.” && $file “..” && !preg_match(“/^hide/i”,$file)){
echo ”;
echo $listnames ? ”.str_replace($folder, ”, $file).” : ”;
}
}
}

say I want to list all jpg with name underneath—

list_images(‘./’,'jpg’, true);

or if I want to list all images with no name output–

list_images(‘./’,'all’, false);

sam
Aug 18th, 2010

feel free to leave a scribble

Name:
Email:
gravatar
Want an image next to your comments?
visit gravatar.com
Message:
Get a free quote