Friday, 3 June 2016

PHP- LIST ALL FILES IN A DIRECTORY

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;
}

0 comments:

Post a Comment