Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I convert a list of files into links?

ColdFusion makes it easy to dynamically list out the files in a folder. Converting this list of files into a list of hyperlinks is pretty simple. This will only work if the folder is under your web root (or available as a virtual folder on your web server). First decide on the folder you will list:

<cfdirectory directory="c:\web\pdfs" name="pdffiles">

Next you need to loop over the files and output them. The link will be based on the folder and how it relates to your web server's document root. In the example above the folder was c:\web\pdfs. The pdfs folder is directly under web root. The links then would simply link to the /pdfs folder with the name being dynamic:

<cfoutput query="pdffiles">
<a href="/pdfs/#name#">#name#</a><br>
</cfoutput>


This question was written by Raymond Camden.
It was last updated on January 21, 2007 at 9:26:47 PM EST.

CFML Referenced

<cfoutput>

Categories

File and Directory Access

Comments

Comment made by John Barrett on January 21, 2007 at 11:41 PM
Hi Ray, This is assume! Thank you:)

A question, if I want to do this for something that is not in the web root, can I create a mapping to point to the files?


Comment made by Seb Duggan on January 22, 2007 at 4:50 AM
Yes, that would work...

Depending on how many sites you are running, though, you may want to create the mapping via the web server (remember that CF mappings will apply to ALL websites).

For instance, if you're running on Windows, you'd want to create a Virtual Directory for that website in IIS.


Comment made by Sid Wing on January 22, 2007 at 8:01 AM
John, We do this a lot. Our document storage is on a NAS device, so in IIS we create a vitural directory off of the webroot and link to those documents that way. It's easy to do, but if you have any questions on how to configure IIS, just give me a shout and I'll help walk you through it.


Comment made by doug on January 22, 2007 at 9:18 PM
I have a component available that does this over at http://dougr.net. The CFC has the form packaged with it (I know - I have been thinking I would move it out of there for best practice but it was more a matter of convenience).


Comment made by Pat Dobson on January 23, 2007 at 3:25 AM
I've used this in the past as well, very useful. Along with the name of the file, you also get further information such as file size and last modified date. I'd suggest (as a test) a "cfdump" of the resultant files (pdffiles in this case) to see exactly what you're getting. It also returns any subfolders so in theory you could map (and navigate) and entire file structure this way.