The PHP script below reads a directory/folder and outputs the file names (18 images) into a nice, semantically-correct, html4strict-validated list. There is another folder which has thumbnail images with the same names.

The gallery idea is not mine - I 'borrowed' it from Stu Nicholls because I think it's great. Thanks, Stu.

What I've done here - the point of this page - is automated it. The point of this is that it saves you typing out all those file-names.

The gallery is here.

At the top of the page there's a path to the top-drawer directory of this site. Just above that is my folder (thumbs72) which has the thumbnails - they're 72px wide and 54px high. For this gallery, there are 18 images in the folder. Then, there are images with the same names in a different folder. They're 500px by 375px and they're in a folder called 'thumbs500'.

In the CSS there's a 'while' loop that basically reads the names of all the files in your folder. Each time it goes through the loop, the value of the variable $i goes up one, so the names of the ids are inserted into the css - #pic1, #pic2, etc. When $i reaches 10, it adds 400px margin-top to the styles so that we can have the images at the bottom.

Further down, in the html, the same thing happens - it reads the directory, finds the names of the files and puts each one into img tags inside li tags to make a nice list. The ids are also inserted into the li tags in the same way as in the css.

You can see in the img tag part of the second loop, there's this : '/thumbs500/$file'. That inserts the big images. The big images are all loaded up, but they are shrunk to 1px dots, so you can't see them. The CSS says that on hover the images will be 500px wide (and placed absolutely in the centre there).

I think that's about it...

<title>CHEERS pics</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
ul, li {
ul {
background:url(cheerslogo500.jpg) 125px 70px no-repeat;
border:1px solid #ccc;
li {
margin:5px 0;
background-position:center center;
while ($file=readdir($handle)) {
if (strpos($file, '.jpg')) {
echo "#pic$i {".(($i>9)? '
margin-top:400px;' :'')."
a {
a img {
* html a:hover {
a:hover img {
a:active img,
a:focus img {
while ($file=readdir($handle)) {
if (strpos($file, '.jpg')) {
echo "<li id=\"pic$i\"><a href=\"#n\"><img src=\"/thumbs500/$file\" alt=\"pic$i\" /></a></li>

In case you're wondering, the pictures are of my school, I'm the fool in the blue shirt and the logo was designed by Ikuko.


2006-08-26 michael says :

i love the concept of this, very handy for allowing a client to ftp photos and let them think they're actually doing something. smile

seriously though, very nice.

however, i'm thinking i'm missing something somewhere. I have gone through it a couple of times, and unfortunately i'm, so far, unable to have the larger photos show other than their ALT tag on rollover.

can't figure out (with my rudimentary phpSkillset) where i've gone wrong.

i know you can't see my 'code' once it loads, but for a glimpse that might assist you in saying "hey michael, you forgot to...", here's the link:

if you're still reading the posts here, thanks in advance for your help!


2007-03-04 Lutfi says :

Hi this is a fantastic display , can you show me how to create the same gallery but using an mysql DB , and using a onclick insted of a o rolloer ?
would really make my day ,i know a lot of people who would benefit from this , i will be happy to spread the word

2007-03-08 BonRouge says :

To do that, just change the while loops to use mysql queries instead of reading the directory and remove the :hover part from the CSS.

2007-09-12 Brian G. says :

The quality of the content of this site never ceases to amaze me. This site is awesome, and I hope you continue to share awesome code with us!

PS. It would be great if you would show us how this comment form works.

God bless,
Brian G.
Temecula, CA

2007-09-20 PZ says :

I'm having lots of trouble with this gallery. I dont know much php so customizing it is getting to be a pain. What I want is for the thumbnails to line up on the left, then the large image to appear on the right. I'm gonna put the thumbnails in a scrolling div that way I can fit a whole bunch. However, I can't even get the thumbnails to show up... aye carumba.

2007-11-13 Barney says :

Thanks for the tips. I finally managed to get this working. It works fine, apart from on Safari, where you can see a 1-pixel wide strip (presumably the 'hidden' images) down the left hand side of the image display area. Do you know of a fix for this small problem?

2007-11-13 Barney says :

Hello - me again!
What dictates the order in which the images are displayed? I presumed it was by filename but this doesn't appear to be true. I cannot work out how to display the images in my desired order. Can you help?

2007-11-14 BonRouge says :

I'm afraid I'm not going to be much help here because I can't test with Safari (as I don't use a Mac) and until you just told me otherwise, I thought the files were appearing by filename too. frown

2007-11-14 Barney says :

Oh bugger.

2007-11-22 Barney says :

Hello again. I am trying to use this system on a page with a site menu, which uses an unordered list. Even if I give my menu a unique id (such as #mainNav), the ul and li parts are affected by the css code used for the auto-gallery. How can I get round this? I treid giving the auto-gallery 'ul' its own id but I am not good enough at php to make this work properly.
Help would be appreciated.

2007-11-30 Barney says :

Thought you might like to see how I implemented this script in my site. I managed to find a way round all the problems I encountered along the way.
Thanks so much for the help! Your site has proven invaluable in its help and advice.

2008-05-19 Jon says :

I love this script, exactly what I was searching for! But the order of the images does seem randomized. Does anyone have an input on that? Even Barney's site looks like the order is randomized...

2009-07-03 dfend says :

Where are the css and html codes for the auto gallery?

2009-07-03 BonRouge says :

That's it right there - the PHP code creates most of the HTML and CSS.

2009-11-18 Tres says :


First off, this looks great and I love the layout. I have a couple questions that hopefully you can answer (they are pretty simple but im a novice at this).

1.) How do I add more photos then just the 18? I did this in the script but they didnt load on to the page.

2.) Is there a way to make it so that the thumbnails on the right and left of the page rather then the top and bottom?

I think that is all for now. Thanks in advance.

2022-09-28 Euuyglavy says :

hydrochlorothiazide mechanism of action hypertension <a href=" ">hydrochlorothiazide and diabetes</a> will hydrochlorothiazide show on drug test

Comment form

Please type the word 'brown' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url