Build a directory browser with ColdFusion

coldfusion

Most web servers have directory browsing disabled, but sometimes you want it to work for certain directories. Most web servers also allow you to setup specific settings for your directories for instance you could do it with an .htaccess file on Apache. But if you don't have access to your web server, you can create a directory index pretty easily with ColdFusion's cfdirectory tag.

I have whipped up a simple one you can use, just save the following code as index.cfm in the directory where you want to see a file listing:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title><cfoutput>#Replace(cgi.script_name, "index.cfm", "")#</cfoutput></title>
<style type="text/css">
	body,td { font-family: verdana; padding: 2px 0px 2px 0px; color:silver;  } 
	th { border-bottom: 1px solid silver;text-align:left;margin-bottom: 2px; color:black; }
	a.sort { text-decoration: none;	color: silver; }
	a:hover { background-color: blue; color: white;	}
	p { font-size: x-small; }
</style>
</head>
<body>
<cfparam name="url.sort" default="datelastmodified desc">
<cfdirectory directory="#ExpandPath("./")#" action="list" name="dir" sort="#url.sort#">

<table width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<th>Name <a href="?sort=name" class="sort" title="Sort By Name">∨</a></th>
		<th>Size (bytes) <a href="?sort=size" class="sort" title="Sort By Size">∨</a></th>
		<th>Last Modified <a href="?sort=datelastmodified+desc" class="sort" title="Sort By Date">∨</a></th>
	</tr>
	<cfoutput query="dir">
	<cfif dir.name IS NOT "index.cfm">
	<tr>
		<td><a href="#dir.name#">#dir.name#</a></td>
		<td>#dir.size#</td>
		<td>#dir.datelastmodified#</td>
	</tr>
	</cfif>
	</cfoutput>
</table>
<p>Directory Browser by <a href="http://www.petefreitag.com/">Pete Freitag</a></p>
</body>
</html>

You can see a slightly modified version of this script on the daily mashup archives page.


cfobjective pre-conf training

Related Entries

6 people found this page useful, what do you think?

WAF for CF

Trackbacks

Trackback Address: 462/8F3A12CEFB439CB07136B30862C8204E

Comments

On 05/01/2006 at 10:30:34 AM EDT chris passmore wrote:
1
What if you want to be able to change that directory in via the browser? Like, if type EQ dir then open that folder and show a new directory, but if it's a file show the file in the browser.

On 05/01/2006 at 10:50:28 AM EDT Pete Freitag wrote:
2
I guess for security reasons I don't allow you to browse just any directory. You could modify the script to allow that, but make sure your not creating security holes.

On 05/01/2006 at 12:15:31 PM EDT chris passmore wrote:
3
This is in a secured area, but I'm having trouble getting this to work. Right now I'm trying to concatenate the folder onto the path... but it's not working just right.

Any thoughts?

On 04/15/2008 at 8:57:38 PM EDT Don Chinnici wrote:
4
Hi Pete. Thanks for your work with this. If you have an opportunity, I'm having some help (as a non-professional) with this. I've tried every iteration to try and get this to work on my site, but with no success. I suppose my first question is: Is it possible that I don't have coldfusion on my site. I know that I do have Apache.

Here's my attempt: http://uffcanadianpartners.com/agent/files/index2.cfm

Perhaps I'm supposed to change some of that stuff to my own site-specific; but I don't know cf to be able to figure out which. Might you have the time for a quick eyeball? Thanks -- Don

On 04/16/2008 at 5:53:07 AM EDT Pete Freitag wrote:
5
Hi Don,

I get a 404 File not Found error when trying to access that file. But chances are if you don't ColdFusion installed. You can get it from Adobe.com

Apache does have a built in mechanism for directory browsing. See my apache cheat sheet for an example: http://www.petefreitag.com/cheatsheets/apache/

On 06/30/2008 at 8:42:40 AM EDT Jerry wrote:
6
Hi Pete, thanks for the code. I set the default sort to filename, but what if I wanted directories listed first (sorted), then the files sorted by name?

Thanks! - J

P.S. Your link to your cheat sheet has a problem.

On 08/29/2008 at 4:44:58 AM EDT Katie A wrote:
7
So useful! Thank your Pete!

On 08/29/2008 at 4:45:34 AM EDT katie wrote:
8
you* (you can also thank your pete, and i shall thank my pete)

On 12/16/2009 at 10:54:37 PM EST Alfio wrote:
9
Pete, Thanks I allowed my CF server to process html files and just thru the script in there for when I'm doing testing and can't remember the directory for a file off the top of my head. It's a great help!

Post a Comment




  



Spell Checker by Foundeo

Recent Entries





Basecamp
pfreitag on twitter