Skip to main content
Inspiring
March 10, 2010
Question

Links with cfdirectory?

  • March 10, 2010
  • 2 replies
  • 1997 views

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?

    This topic has been closed for replies.

    2 replies

    cwhitmorAuthor
    Inspiring
    March 11, 2010

    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.

    ilssac
    Inspiring
    March 11, 2010

    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.

    ilssac
    Inspiring
    March 11, 2010

    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.


    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?

    ilssac
    Inspiring
    March 10, 2010

    *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.

    cwhitmorAuthor
    Inspiring
    March 10, 2010

    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.

    ilssac
    Inspiring
    March 10, 2010

    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>