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.
Nick

Recommended Answers

All 4 Replies

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.

<?php
// 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;
		}
	}
	closedir($handle); 
}
// 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;
?>
commented: This post was exactly what I needed to know! +1

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

No worries - happy to help

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

if(!($dp = opendir($image))) die ("Cannot open Directory.");
$i=1;
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;
			}
			$i++;
		}

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


closedir($dp);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.