Pete Freitag Pete Freitag

Getting the Application Root Path in ColdFusion

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.


Like this? Follow me ↯

Getting the Application Root Path in ColdFusion was first published on April 09, 2007.

If you like reading about application.file, expandpath, cfml, or coldfusion then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

CFBreak
The weekly newsletter for the CFML Community


Comments

Send to cflib?
by Raymond Camden on 04/09/2007 at 3:19:42 PM UTC
Awesome! Thanks! Where were you a few months ago when I really needed one of these?! Just kidding, thanks for sharing.
by Boyan on 04/09/2007 at 6:03:58 PM UTC
Pete, nifty little function, thanks, it just saved me some time.
by Rob Munn on 02/20/2008 at 2:32:44 PM UTC
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)
by Chris on 12/26/2008 at 5:04:53 PM UTC
I know this is an old post, but I modified the script so that I could add it to the CustomTag directory and identify a shared IIS virtual directory on my server that exists outside of the application root. The technique for going down a sub-directory (../) wasn't working as I was using a full path.

I used the "ListReverse" UDF to safely identify and drop the current sub-directory.
http://cflib.org/udf/ListReverse

Here's the code:
<cffunction name="getScriptsGlobalRootPath" returntype="string" hint="Returns the root path of the shared virtual script directory." output="false" access="public">
<cfargument name="base_path" default="#GetDirectoryFromPath(getBaseTemplatePath())#" hint="Normally you want to leave this as default" required="false">
<cfset var actual_path = arguments.base_path>
<cfif DirectoryExists(arguments.base_path & "_globalScripts")>
<cfreturn actual_path & "_globalScripts">
<cfelseif listlen(actual_path,":") GT 1>
<CFSET arguments.base_path = Request.ListReverse(ListRest(Request.ListReverse(arguments.base_path, "\"),"\"),"\") & "\">
<cfreturn getScriptsGlobalRootPath(arguments.base_path)>
<cfelse>
<!--- we have reached the root dir, so throw an error not found --->
<cfthrow message="Unable to determine the shared virtual script directory" detail="#actual_path#">
</cfif>
</cffunction>
by James Moberg on 05/11/2010 at 2:27:57 PM UTC