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.
Like this? Follow me ↯Tweet Follow @pfreitag
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:
- Top 10 Underrated Functions in ColdFusion
- Dumping HTTP Servlet Request Attributes and Parameters in CF
- 6 ways to stay connected with other CF developers
- ColdFusion Function Arguments now support Integer
- Fixinator and Foundeo Security Bundle
- CFSummit 2016 Slides
- Scope Injection in CFML
- CFDocs site now Open Source
The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.