pf » Getting the Application Root Path in ColdFusion
April 09, 2007
Getting the Application Root Path in ColdFusion
I wrote a handy function today that will return the server file system path of the acting Application.cfm file for a ColdFusion application. It works by working its way up the directory tree until it finds an Application.cfm file. If it doesn't find one it will throw an exception.
<cffunction name="getApplicationRootPath" returntype="string" hint="Returns the root path of the acting Application.cfm file." output="false" access="public">
<cfargument name="base_path" default="./" hint="Normally you want to leave this as default" required="false">
<cfset var actual_path = ExpandPath(arguments.base_path)>
<cfif FileExists(ExpandPath(arguments.base_path & "Application.cfm"))>
<cfreturn actual_path>
<cfelseif REFind(".*[/\\].*[/\\].*", actual_path)>
<cfreturn getApplicationRootPath("../#arguments.base_path#")>
<cfelse>
<!--- we have reached the root dir, so throw an error not found --->
<cfthrow message="Unable to determine Application Root Path" detail="#actual_path#">
</cfif>
</cffunction>
It probably could be optimized a bit more, but it does the trick for me.
Related Entries
- Top 10 Underrated Functions in ColdFusion - January 10, 2007
- Mastering CFQUERYPARAM - July 24, 2008
- Getting ColdFusion SQL Statements from SQL Server Trace - June 16, 2008
- CFSCRIPT Cheatsheet - May 5, 2008
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks - April 5, 2008
Trackback Address: 630/9670773582D93C722429E61A9ABCCE4D
Comments
On 04/09/2007 at 3:19:42 PM MDT Raymond Camden wrote:
1
Send to cflib?
On 04/09/2007 at 6:03:58 PM MDT Boyan wrote:
2
Awesome! Thanks! Where were you a few months ago when I really needed one of these?! Just kidding, thanks for sharing.
On 02/20/2008 at 2:32:44 PM MST Rob Munn wrote:
3
Pete, nifty little function, thanks, it just saved me some time.
On 05/08/2008 at 10:33:10 AM MDT magruth wrote:
4
This was a great way for me to determine what environment my application was running in. Thanks!
On 12/26/2008 at 5:04:53 PM MST Chris wrote:
5
alternately you could put this in the application file itself
<cfset request.appPath = GetCurrentTemplatePath()>
That will give you a request var with the same info in it (server path to the application)
- ColdFusion 8 FCKeditor Vulnerability
- Ajax Same Origin Policy No More with Firefox 3.5
- Firefox 3.5 Introduces Origin Header, Security Features
- Tips for Secure File Uploads with ColdFusion
- 7 Years And Blog Entry Number 700
- CFCatch Java Exceptions
- Cheat Sheet for SQL Server
- CFML on Google App Engine for Java
Subscribe to my RSS Feed:
RSS
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in











