ColdFusion ExpandPath() And GetCurrentTemplatePath()
Posted September 21, 2006 at 3:33 PM
One of the things that always get's me is the fact that in ColdFusion, the ExpandPath() method expands the path of the base path. I would say 95% of the time, this doesn't matter, but the other 5% of the time, I end putting stuff in the wrong directories. In my head, it just makes sense that ExpandPath() should expand the current template path since that is the one calling the code (just how the CFInclude template attribute works).
When I do need to expand the current path, not the base one, I just use the GetCurrentTemplatePath(). And, since expand path is meant to add to a directory, I get the directory from the returned path:
Launch code in new window » Download code as text file »
- <!--- Get expanded path of the BASE path. --->
- <cfset strPath = ExpandPath( "./" ) />
-
- <!--- Get the expanded path of the CURRENT template path. --->
- <cfset strPath = GetDirectoryFromPath(
- GetCurrentTemplatePath()
- ) />
Assuming that we have the current directory structure:
sites
..test
....index.cfm
....sub
......test.cfm
and that our CGI.script_name points us to the page "index.cfm" but the code is being executed by the CFIncluded page, test.cfm, the first path would be:
D:\sites\test\
and the second path would be:
D:\sites\test\sub\
So anyway, just keep that in mind. We use CFIncludes so much in ColdFusion that I feel it easy to make this mistake.
Download Code Snippet ZIP File
Post Comment | Ask Ben | Other Searches | Print Page
Newer Post
SQL Optimization And ON Clause vs WHERE Clause
Older Post
Project HUGE : Physical Therapy Is Over
Reader Comments
Thanks Ben - your Blog has to be the most useful resource out there when it comes to CF Quirks - I remember reading this entry a couple of years back - first port of call when I needed to do this...
@Dan,
I am always happy to hear that I have provided some value - thanks.
Hopefully this tip will help someone down the road too, or maybe someone can enlighten me as to the "right" way to code for the physical file path of an application.
I was setting the following code in the OnApplicationStart() method to set the application's physical file path (http://mydomain.com/myApplication):
<cfset application.rootpath = expandPath('./')>
The application uses index.cfm as a "controller" (i.e.index.cfm?disp=thispage) so I THOUGHT it would set the path relative to the root of the application (C:\inetpub\wwwroot\mydomain\myApplication)
Well, it does...MOST of the time....I still haven't gotten to the bottom of it, but sometimes the rootpath would get set to nested directories (C:\inetpub\wwwroot\mydomain\myApplication\middle\). I can only assume that the application would sometimes reinit when it was in the middle of processing something in the "middle" directory.
Long story short, I'm about to deploy this code fix to (hopefully) resolve the problem: <cfset application.rootpath = "#expandpath('/')##application.applicationname#\"> . The application.applicationname is the same as the "myApplication" directory, so I'm hopeful that this will work....at least my three test runs worked..lol
With any luck, all this code will actually show up so its readable on Ben's blog :)
@Dan,
The strategy that I have found to be very helpful is using:
getDirectoryFromPath( getCurrentTemplatePath() )
... inside of Application.cfc. Since you always know where the Application.cfc file lives, you know you can get the root directory by stripping off "Application.cfc" from the current template path (which is Application.cfc).



