For the most part, I try to discourage users from browsing around the
directory structure of the sites we develop. There are times however when
letting a user look around and see what files are there is helpful. You
could simply allow directory browsing for that directory, but then you've got
to remember that you set that setting so you or someone else doesn't
accidently override it not to mention that it's not very nice looking to drop
from your custom designed extranet frontend to IIS's pathetic looking
directory list. Not like I really care (heck I'd rather have the bare bones
display and save the bandwidth) but stop and think about your visitors for a
second. You know, the ones who go ooh and aah over your pretty color scheme!
This script is just a quick example of a way to get the best of both worlds.
You can get the nice display, but not have to maintain the links since
they're generated from the directories and files themselves! Add a new file
and like magic it appears on the page. Delete one and... well you get the
idea!
This script should work as is for displaying one directory. Just set the
directory you want to show. You may want to create your own icons or add
file types, but those are left up to you. The next natural progression would
be to take the folder links and pass them back through the script so that
people could browse the directory tree of your entire site all from this one
little page. Cool huh! A little scary too, but cool none the less!
Update: Recursive Version
One of our most frequently asked questions comes from this very sample.
The question everyone seems to want answered is how do you write a
version of the script that will traverse sub-folders that I mentioned above.
It's really quite simple... take a look at the code
and see for yourself.
I don't have this particular script running live on the server,
but you can download a zip file containing it from here:
dir_list_recursive.zip (1.3 KB).
I've also written an ASP.NET 1.x version for those of you using ASP.NET.
You can find it here: dir_list_recursive_aspx.zip (1.3 KB).