# display all images in a folder - help needed



## gurutech (Apr 23, 2004)

I'm trying to write a quick php or html code that will scan a specific folder on my website and display these files.

File names won't change, but each of the images is a PNG file with text (which changes) written over the image via apache "rewrite" code.

Each time I pull up an individual image file, everything works fine. But I would like to have a single page that will "pull up" all of the PNG files in a single folder (about 10-15 images total).

With the code I have, I just get a blank screen. I'm using the "foreach" and "glob($dir.$filename)" functions, if that helps.

Can someone point me in the right direction to get this working?


----------



## JiminSA (Dec 15, 2011)

Hi there I actually use the following script to display car photo uploads which can can be jpg, gif or png, but you can adapt it for your own requirements, I'm sure...

```
$photo_dir = "FU/server/php/files/" . $id . "/medium/";
if (is_dir($photo_dir))
{
	if ($handle = opendir($photo_dir)) 
	{
		while (false !== ($entry = readdir($handle))) 
		{
			if ($entry[0] != ".")
			{
				echo "[TD][URL]<img src=\"$photo_dir$entry\" class=\"upped\" title=\"Click to view Gallery...\" alt=\"Large Car Pic\" width=\"640\" />[/URL][/TD]";
				break;
			}
		}
		closedir($handle);
	}
}
```
in each windows directory there are a couple of control files called . and .. - that is the reason for the check on the first character of the name of the file.


----------



## gurutech (Apr 23, 2004)

I'm actually looking for the script to display the image file itself, not the name of the image file in a directory type listing.

Here's what I have so far:


```
<html>
 <body>
    <?php
	$dir = "http://site.server.com/images/";
	
    $images = glob($dir . "*.png");

    foreach ($images as $image) 
	{
    echo "<img src='$image' >";
    }
    ?>
</body>
</html>
```


----------



## JiminSA (Dec 15, 2011)

Sometimes when we use image editors they are saved with the file extension CAPITALIZED - so, also search for "*.PNG" images...


----------



## ehymel (Aug 12, 2007)

glob will not work on remote servers. You will have to set $dir to be a local directory on your server to work.

Alternatively, use JiminSA's script, changing the <a href> tags to <img> tags to get what you want.


----------



## gurutech (Apr 23, 2004)

ehymel said:


> glob will not work on remote servers. You will have to set $dir to be a local directory on your server to work.
> 
> Alternatively, use JiminSA's script, changing the <a href> tags to <img> tags to get what you want.


The $dir path is technically on the local server, just in the format of a full URL. I'll see how I can change that around to try it as a true "local" path - maybe just using "./folder" or /folder will work.


----------



## JiminSA (Dec 15, 2011)

Yes it will work on localhost, as well as on your public domain, without modifications - you simply point your <img src= relative to your html pages location... e.g. if your html folder is way down deep in your website structure (for example htdocs/pages/html/ and the image folder is in a different directory then by starting your url path with ../ you direct the path starting from your base directory viz:- src="../graphics/site_images/fred.png". So by using ../ we direct the url relevant to the base directory.
If we omit the ../ then the url path is relevant to your html's directory.
Hope that helps...


----------

