Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Can I create an Apache image directory browser script?

I run a fairly busy Web site with a variety of different image directories. The problem is, Apache shows directories in a fairly rudimentary way which makes it quite difficult to find a specific image if I don't remember its name. Is there a better way with some sort of shell scripting?


Dave's Answer:

This is a splendid chance to really show off just how powerful and versatile shell scripts can prove for basic Linux interaction, actually, so let's jump in.

First off, when you view a directory in Apache (that is, from within your browser while connected to a Web server running Apache) it displays a listing similar to:

Apache Directory Listing

You can see, it's hard to know what's what, particularly if you're not as disciplined with your file naming conventions. How much more useful to have some sort of thumbnail previews of the images in the page!

Turns out you can do just that with a simple shell script named as index.cgi in the images directory (well, you might have to slightly tweak your httpd.conf configuration, but fundamentally it should be easy).

Here's how that script, at its most fundamental, would look:

#!/bin/sh

echo "Content-type: text/html"
echo ""

for name in *
do
  echo "<img src=$name><br />$name<br />"
done

exit 0

This script is pretty crude, I admit, because it assumes that everything it matches is an image suitable for being displayed with the HTML "img" tag. If that's true, well, this works just fine, and here's what you get:

Apache Directory Listing, Improved!

That's just the beginning of things you can customize or fine-tune, actually. I have another version of the script on my server that's almost 100 lines long and does all sorts of nifty tricks. Interested? Pay attention to my column in Linux Journal and you'll see it show up there! :-)

One thing to consider: in most modern browsers, specifying a height only or width only in an img tag automatically scales the image down to that size, but proportionally. That makes it pretty straightforward to ensure no one image is too darn big in your directory listing, right? This also means that you could turn this simple script into a basic photo browser too!

Anyway, this should be enough to get you started with a simple image display script on your own Web server!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

hi,
your linux journal link points back to this same article.
thanks.

Posted by: Prashanth Narayanan at October 24, 2006 12:26 PM

Hey Dave, I do a lot of work creating online image galleries with php/mysql. However, I'd never given much thought to resurrecting my shell scripting (which I loved through the ages). All the sites that I maintain are hosted on linux or bsd systems, so this is easy. I figured out how to use 'imgsize' to determine if the images are portrait or landscape, then added a little CSS for backgrounds, margins and such and I have a less 'crude' yet palatable image gallery. I can post asn example if it interests you. -Bob

Posted by: Bob Meetin at March 8, 2007 7:46 PM

-> Prashanth

I would imagine, that the link was not ready as the time of writing this article. You should be able to find it at the following URL, when the magazine is published:

http://www.linuxjournal.com/article/9622

./h

Posted by: henrik hammer at April 28, 2007 8:55 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]