Created April 14 2001

PHP AutoMagic Gallery Listing


This is a small project driven by my desire to have a simple page I could drop into a folder full of images and get a clean, simple listing of the images, plus links to the images in a way that was somewhat attractive.

See it in action at

Feel free to use it -- just copy and paste the source below. use a filename that's the default file format that processes php. Usually index.php or index.php3. The script doesn't care either way.

What do you think?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
   <title>a picture is worth a thousand words</title>
<body bgcolor="#ffffff">
<!-- I'm sorry, I include the height attribute in the table tag.       -->
<!-- it is the only thing which will prevent this page from validating -->
<!-- @                                        -->
<table width="100%" height="94%" border="0" cellspacing="0" cellpadding="0">
      <td align="center">
if (!isset($i)) { // this first bit determines if the page got passed $i
   $directory = "./"; // this decides what directory we want to work on
   exec("ls -r $directory*.jpg", $contents); // list the jpegs (modify as appropriate)
   $i = 0;
   while (($contents[$i])) // this bit makes links for all the images
       echo "<a href='./?i=$directory$contents[$i]'>$contents[$i]</a><br>\n";
   $noback=1; // if we're listing images, we don't want to go back
} elseif (is_file($i)) { // is $i an image in the filesystem? this is important!
   $size = GetImageSize($i); // grabs the height and width tags, providing an html
                             // fragment we will use later. best web practice is to 
                             // include height and width tags in your images
<?php // and we finally write the html for the image. First the src,
      // then the height and width, and a pretty border. Season to taste. ?>
<img src="<?php echo $i; ?>" <?php echo $size[3]; ?> alt="" border="5">
<?php // if $i is not a file, we may have url hacking, or a broken URL,
      // maybe from an emailed link? In any case, the [ main ] link
      // will take them the main directory listing where they
      // can get at your pictures. so maybe it's not as stupid
      // a program as it claims to be. ?>
<?php } else { ?>
            there's something wrong here.<br>
            unfortunately i'm a stupid program<br>
            and i don't know what the problem is.<br>
<?php } ?>
<?php if ($noback==0) { ?>
      <td valign="bottom" align="center">
            <a href="./">[ main ]</a>
<?php }; ?>

<!-- this by Joe Crawford, April 2001 -->