
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? 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: ![]() 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: ![]() 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 Stumble Upon
Categorized:
CGI Scripts and Web Site Programming
,
Shell Script Programming
(Article 6934)
Tagged: apache, httpd.conf, image directory, shell script programming Previous: How can I calculate the value of a business? Next: Are sites like pspblender and allpspgames legal? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. hi, 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 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|