Hi everyone,
I am trying to make a php web page that displays images. Is there any way I can use php to look at what is in a certain folder(that only contains images) and then have it write html to display all the images from that folder on the page? I've been looking but I'm not sure what to be looking for or if it is even possible. Thanks for any help in advance.

This is quite a basic script that will read the contents of a folder and display the images in that folder. It has no validation, formatting, or other required functions, but should give you an idea on where to start.

// set a directory folder to search. Either use a fully qualified path or a relative path
$dir = "images/";
// get the folder contents
if ($handle = @opendir("$dir")) {
	while (false !== ($file = readdir($handle))) { 
		if ($file != "." && $file != ".." && strpos($file, '.')>0) { 
			$imgarr[] = $file;
// now test to see if any images are in the array
$output = null;
if (count($imgarr)>0) {
	for ($i=0,$n=count($imgarr);$i<$n;$i++) {
		$output .= "<img src=\"$dir$imgarr[$i]\" />";
echo $output;
This post was exactly what I needed to know!

That was exactly what I was looking for. Thank you very much.

if(!($dp = opendir($image))) die ("Cannot open Directory.");
echo "<TABLE align=center text-align=center><CENTER>";
echo "<TR>";

	while($file = readdir($dp)){
		if($file != '.' && $file != '..')
			echo "<TD>";
			echo "<a href='$image/$file' target='_blank'><img src='$image/$file' width='120' height='120' alt='image'><br>$file</a>\n";	
			echo "<a href='downpic.php?imgD=$file'>download</a><br>\n";	
			echo "</TD>";

			if($i == 5)
				echo "<TR>";
				$i = 0;

echo "</CENTER></TABLE>";

This article has been dead for over six months. Start a new discussion instead.