Copy link to clipboard
Copied
I would like to setup a directory listing with cfdirectory so anytime someone goes
to this page the current list of directories and files underneath it are listed and linked so they can click and open the documents.
How can I setup links with cfdirectory? If that's not possible what other tag can I use that with dynamically check the directories and list the files?
Copy link to clipboard
Copied
*IF* you do not want to use the directory browsing features built into most Web Servers, you can definitely use the <cfdirectory...> functionality for this.
There are some obvious caviot related to the reletive difference between the system file structure that <cfdirectory...> access and the web root file structure the HTML links are going to use. So, if you are just using the <cfdirectory...> tag to list and display the contents of the web root file structury, you will just need to translate the file path to an URL path. This would simply involve removing the beginning of the path from the string to use it for the URL. I.E. The file path of a common windows web root index file would be "C:\inetpub\wwwroot\index.html" You would need to remove the beginning of that path to reduce it to just "index.html" to make it an appropiate URL path.
IF you are planning to use the <cfdirectory...> tag to display file contents that are not under the webroot, then you will not be able to create a simple URL that would access those files. Not unless there was a virtural directory mapping in the web server that connects to this file location outside the web root. If this is true, you will need logic that will translate the file path to the appropiate virtural directory string.
Otherwise you will need some file delivery mechnism to deliver content that is not accessible to the web server. This can be done with the <cfcontent...> tag. There are plenty of information about using this tecnique in the documenataion, this forum and other web blogs, lists and other resources.
Copy link to clipboard
Copied
Thanks for the reply. I would love to use the simple file directory structure in Windows, but they want these folders
password protected so I'm also using a simple security check and logon page to get to this area.
After your explanation I can see why this isn't as easy as I thought it would be. I'll have to look for an alternative way since I don't have the time or skill to create a custom tag that will rename the URL for each file.
I was hoping to have something that would allow me to post files and folders to this area and have it dynamically updated on the CF page.
Copy link to clipboard
Copied
It's not all that hard.
Here is a dirty little directory lister I just whipped up since I was bored. I suspect there are prettier and more featured ones available ot the http://www.cflib.org/ web site.
<cfparam name="url.dir" default="C:\inetpub\wwwroot\">
<cfdirectory action="list" name="dirQry" directory="#url.dir#">
<ul>
<cfoutput query="dirQry">
<cfif dirQry.type eq "file">
<li><a href="#replace(dirQry.directory,'C:\inetpub\wwwroot','','ONE')#/#dirQry.name#">#dirQry.type# #dirQry.directory#\#dirQry.name#</a></li>
<cfelse>
<li><a href="dirList.cfm?dir=#urlEncodedFormat(dirQry.directory & '\' & dirQry.name)#">#dirQry.type# #dirQry.directory#\#dirQry.name#</a></li>
</cfif>
</cfoutput>
</ul>
Copy link to clipboard
Copied
Ian,
Thanks for your help. This is exactly what I need. The file links work great, but how can I display the contents of the folders when I access them?
Thanks again,
Carlton.
Copy link to clipboard
Copied
I'm not sure what you are asking? That code would display the contents of a directory if you clicked on a *DIR* link.
It's not a pretty display, the directory and the files are all mixed together.
There's no navigation built ino so that once you are in a sub-directory the only way up to the parent directory is the browser BACK button.
If you want all the directory and sub-directories listed on the inital page, just add the recrusive paramter to the <cfdirectory...> tag.
If you only want to list files (or just list directories) use the filter properties of the <cfdirectory...> tag.
You will probably want to pretty up the output.
As I said before, it would not surpise me if this code is not already written for you out in the internets.
Copy link to clipboard
Copied
When I use your code and try to open one of the directories I get an error that it can't find Listdir.cfm. Do I need to create this file for the listing?
Also, I tried using recurse=yes to open all of the folders, but when I try to open one of the files it doesn't include the directory name so the file can't be found.
Any ideas?
Thanks again for your help.
Carlton.
Copy link to clipboard
Copied
cwhitmor wrote:
When I use your code and try to open one of the directories I get an error that it can't find Listdir.cfm. Do I need to create this file for the listing?
The file is calling itself to process sub-directories. So you either need to name the file with my code listdir.cfm or to change the file in the <a>nchor tag to the filename you used for your code.
Also, I tried using recurse=yes to open all of the folders, but when I try to open one of the files it doesn't include the directory name so the file can't be found.
Any ideas?
I would have thought that this code would work with the recursive parameter; as long as we are talking about files available inside the web root. If you are trying to do this with files that live in other directories, then this simple code is not going to work.
Copy link to clipboard
Copied
Ian,
After renaming Listdir.cfm to index.cfm in the anchor tag I can now drill down on the directories. The only issue I have now is when I do that none of the file URLs are working. (example http://www.domain.com/files/TX/test.pdf tries to open http://www.domain.com/files//test.pdf ). For some reason it's skipping the directory name. I tried adding #dirQry.directory# to the end of the base URL, but got errors.
What am I missing here?
Carlton.
Copy link to clipboard
Copied
You have adjusted the "C:\inetpub\wwwroot" string to match your envornment correct?
The code works correctly on my system. You would need to let me know how your system is configured. What is your webroot? What is your base directory?
Copy link to clipboard
Copied
Not sure why, but I had to add the base URL & dirQry.directory at the beginning of the replace statement.
Everything is working fine now.
Thanks for your help on this!