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.
Tweet
Related Entries
- Top 10 Underrated Functions in ColdFusion - January 10, 2007
- Client Variable Cookie CFGLOBALS Includes Session Ids - July 14, 2011
- Maximum Security CFML - cfObjective Slides - May 17, 2011
- Writing Secure CFML Slides from CFUnited 2010 - August 5, 2010
- 10 Ideas to Improve Security in ColdFusion 10 - June 18, 2010
Trackbacks
Comments
<cfset request.appPath = GetCurrentTemplatePath()>
That will give you a request var with the same info in it (server path to the application)
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>
Post a Comment
Recent Entries
- Nginx redirect www to non www domain
- HashDOS and ColdFusion
- HackMyCF Updated for APSB11-29 Security Hotfix
- Adobe eSeminar on FuseGuard
- Determining Which Cumulative Hotfixes are Installed on ColdFusion
- Adding Two Factor Authentication to ColdFusion Administrator
- ColdFusion Developer Week at Adobe.com
- Bug Loading Scripts for CFFileUpload and CFMediaPlayer


add to del.icio.us



