View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > ExpandPath PreviousNext

ExpandPath

Creates an absolute, platform-appropriate path that is equivalent to the value of relative_path, appended to the base path. This function (despite its name) can accept an absolute or relative path in the relative_path attribute

The base path is the currently executing page's directory path. It is stored in pageContext.getServletContext().

A string. If the relative path contains a trailing forward slash or backward slash, the return value contains the same trailing character.

System functions

ExpandPath(relative_path)

FileExists, GetCurrentTemplatePath, GetFileFromPath

ColdFusion MX: Changed behavior for the relative_path attribute: this function can now accept an absolute or relative path in the relative_path attribute. To resolve a path, this function uses virtual mappings that are defined in the ColdFusion Administrator. This function does not reliably use virtual mappings that are defined in IIS, Apache, or other Web servers.

Parameter

Description

relative_path

Relative or absolute directory reference or file name, within the current directory, (.\ and ..\) to convert to an absolute path. Can include forward backward slashes.

If the parameter or the returned path is invalid, the function throws an error.

These examples show the valid constructions of relative_path:

<h3>ExpandPath Example - View Only</h3>
<!---
<cfset thisPath=ExpandPath("*.*")>
<cfset thisDirectory=GetDirectoryFromPath(thisPath)>
<cfoutput>
The current directory is: #GetDirectoryFromPath(thisPath)#

<cfif IsDefined("form.yourFile")>
<cfif form.yourFile is not "">
<cfset yourFile = form.yourFile>
   <cfif FileExists(ExpandPath(yourfile))>
   <p>Your file exists in this directory. You entered
   the correct file name, #GetFileFromPath("#thisPath#/#yourfile#")#
   <CFELSE>
   <p>Your file was not found in this directory:
   <br>Here is a list of the other files in this directory:
   <!--- use CFDIRECTORY to give the contents of the
   snippets directory, order by name and size --->
   <CFDIRECTORY DIRECTORY="#thisDirectory#"
   NAME="myDirectory"
   SORT="name ASC, size DESC">
   <!--- Output the contents of the CFDIRECTORY as a CFTABLE --->   
   <CFTABLE QUERY="myDirectory">
   <CFCOL HEADER="NAME:"
         TEXT="#Name#">
   <CFCOL HEADER="SIZE:"
         TEXT="#Size#">
   </CFTABLE>
   </cfif>
</cfif>
<cfelse>
<h3>Please enter a file name</h3>
</CFIF>
</cfoutput>

<FORM action="expandpath.cfm" METHOD="post">
<h3>Enter the name of a file in this directory <I>
<FONT SIZE="-1">(try expandpath.cfm)</FONT></I></h3> <INPUT TYPE="Text" NAME="yourFile"> <INPUT TYPE="Submit" NAME=""> </form> --->

Contents > CFML Reference > ColdFusion Functions > ExpandPath PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


rdeguzman said on Dec 5, 2003 at 6:54 AM :
ExpandPath(" ") would yield different results from ExpanPath(""). Please take note of the difference.
S said on Mar 5, 2004 at 9:17 AM :
I've found that expandPath() doesn't always work. If are using IIS and have a template called from within a virtual directory it will not return the correct path.
This can cause nasty little bugs to crawl into your system if you have something like an image upload. The upload will proceed without error, but the image will not end up in the directory where you expect it.
The only time you will find out is if you browse a page that is trying to display the image.

A 100% reliable method of getting the correct path is either of the following:

If you want the path to the directory containing the currently executing template:
<cfset path = replace(getCurrentTemplatePath(),getFileFromPath(getCurrentTemplatePath()),'')>


If you want the path to the directory containing the template that was called directly on the URL:
<cfset path = replace(getBaseTemplatePath(),getFileFromPath(getBaseTemplatePath()),'')>

Spike
smckeetx said on Jun 17, 2005 at 5:41 AM :
"To resolve a path, this function uses virtual mappings that are defined in the ColdFusion Administrator."

While this seems to work fine on Linux with the enterprise edition it appears to fail on Windows with the developers edition. The mapping is not expanded it is left as is and the path to the server root is preprended to it.
le5 said on Jul 6, 2005 at 1:05 AM :
under MX 6.x (IIS/W2K) ExpandPath() would get right absolute path.
Now with MX 7.x (IIS/W2K) ExpandPath() gets the path from InetPub (Default-Server)
Ugly mistake...

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi78.htm