Experiencing Problem With ColdFusion's ExpandPath('/') Function

For some strange reason, I've been experiencing a (very weird) problem with the ColdFusion function ExpandPath('/'). I have several web sites on various ColdFusion servers in a shared-host environment. Under normal circumstances, ExpandPath('/') would return the base absolute path for the web site.

For example, if you try using ExpandPath('/') in the default ColdFusion installation, it would most likely return "C:\ColdFusion8\wwwroot\"

However if a site is set up in a multi-instance environment (versus the default installation), ExpandPath('/') should return something similar to: "D:\Inetpub\domainname\"

Where I'm confused is why ExpandPath('/') would return "C:\ColdFusion8\wwwroot\" in a shared-host environment. It just doesn't make sense. Stranger still is if I try ExpandPath('/index.cfm') the shared-host server actually returns the correct response of "D:\Inetpub\domainname\index.cfm"

Why do I care? Well, I would be surprised if I'm the only ColdFusion developer who uses ExpandPath('/') in their programming. I usually store this handy little function in a variable and call it out from time to time in various parts of my applications for those times when I need an "absolute, platform-appropriate path." There are many functions that require the absolute path including the drive, directory, etc.

For now, I've come up with a hack to address this but I would prefer to find out what the cause of the problem is.

Option 1, possibly the easiest to implement, but not as dynamic:


<cfset request.RootDirectory = "D:\Inetpub\domainname\" />

Option 2, easier to replicate throughout multiple sites without have to retype the actual path:


<cfscript>
    request.RootDirectory = expandpath("/index.cfm");
    x = listlen(request.RootDirectory, "\");
    request.RootDirectory = listdeleteat(request.RootDirectory, x, "\");
    request.RootDirectory = request.RootDirectory & "\";
</cfscript>

Obviously, none of these are as easy as what I would prefer to do:


<cfset request.RootDirectory = ExpandPath("/") />

Comments

I'd bet that somewhere there is a mapping for '/' that points to the dir you're seeing....
# Posted By todd sharp | 11/26/08 10:26 AM
what about expandpath(".") ?
# Posted By Joshua Cyr | 11/26/08 10:26 AM
i absolutely love the ColdFusion community!

@todd, i agree, but cannot confirm from the host.

@Joshua, just tried it and that works! thank you!

but I wish I knew for sure why this happens from time to time.
# Posted By Stephen Withington | 11/26/08 10:32 AM
My guess is the same as Todds. Most likely the mapping.
# Posted By Joshua Cyr | 11/26/08 10:35 AM
Just to be clear, I had to do this for it to work:

<cfset request.rootDirectory = expandPath(".") & "/" />
# Posted By Stephen Withington | 11/26/08 10:37 AM
The first time some one showed me expandpath, I had this happen to me. I kept trying to get it to work, but after so long I just dropped using it, because i assumed either I wasn't using it properly or something was wonky and it would get fixed later.
# Posted By DevLano | 11/26/08 10:37 AM
I have seen some use expandpath("./") wonder if that would also work for you.
# Posted By Joshua Cyr | 11/26/08 10:39 AM
<cfset factory=createObject("java","coldfusion.server.ServiceFactory")>
<cfset mappings = factory.runtimeService.getMappings()>
# Posted By todd sharp | 11/26/08 10:41 AM
If there isn't a specific mapping causing the issue, you could try setting a mapping for root to your shared site folder.
# Posted By Dave Konopka | 11/26/08 10:46 AM
@Joshua, thanks again! Yes, using expandpath("./") also worked too.

Now it's <cfset request.rootDirectory = expandPath("./") />

@todd, yeah, i got a big ol' "Permission denied for creating Java object" from my host. thanks though!
# Posted By Stephen Withington | 11/26/08 10:57 AM
@DevLano, don't give up on expandPath(), it's a wonderful and very useful tool ... when you can get it to work right.

@Dave, thanks for the tip.
# Posted By Stephen Withington | 11/26/08 10:59 AM
@Joshua,
Well, what you suggested works, kinda. Problem is, it returns the path to where the template is located which _may not_ be the "root" So I guess I'm back to my original hack for now.
# Posted By Stephen Withington | 11/26/08 11:16 AM
I'm a big fan of this (in Application.cfc/cfm):

request.rootDirectory = GetDirectoryFromPath( GetCurrentTemplatePath() ) ;
# Posted By Ben Nadel | 11/26/08 11:16 AM
@Ben, I like yours too ... but it will return the same as expandpath("./"). So for example, if my globalvars.cfm file is in "/extensions/includes/" which is _then_ included in the Application.cfc, request.rootDirectory will return something like "C:\inetpub\domainname\extensions\includes\ "
# Posted By Stephen Withington | 11/26/08 11:21 AM
Stephen, are you by chance with CrystalTech?

One of my sites was working fine for months and suddenly broke yesterday for no reason with the IDENTICAL situation you had.
# Posted By Tariq Ahmed | 11/26/08 12:57 PM
@Tariq, yep. I have an open ticket on it and this isn't the first time it's happened either.
# Posted By Stephen Withington | 11/26/08 1:49 PM

© 2026, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.