Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map path 1

Status
Not open for further replies.

TruthInSatire

Programmer
Aug 12, 2002
2,964
US
I asked this question a while back when I was first learning CF transitioning from ASP. I didn't get an answer, but now that I know more about CF I am able to answer it. The question was: "Is there an ASP equivalent function for "server.mapPath()" in ColdFusion?"

The closest thing was expandPath(). The problem with expandpath(), is it begins in the current folder the template resides in... That means you have to know how many levels up or down you need to go. This can be bothersome if you're several levels above or below your desired folder.

Server.mapPath() begins maping from the webs root all the time despite what folder the asp file is in. This is very helpful for code used over and over in different folder levels where files are being manipulated with File system object(ASP) or <cfFile>(CF)

I've been doing allot with <cffile> lately so I decided to revist the issue. I've come up with the following script which could easily be made into a tag if desired.

With all that said if there is an easier or more foolproof way to do this let me know.
Code:
<cfscript>
  function mapPath(folder){
    siteRoot = mid(cgi.PATH_TRANSLATED, 1, findNoCase(replaceNoCase(cgi.PATH_INFO, "/", "\","All"), cgi.PATH_TRANSLATED));
    userPath = siteRoot & replaceNoCase(folder, "/", "\","All");
    //return the new path
    return (userPath);
  }
</cfscript>
  <cfoutput>#mapPath("family/images/")#</cfoutput>
in this example the output would* be "c:webspace\ assuming "c:webspace\ is where your website is on the server.


* would could easily be swapped with "should"

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Its a monday.. I can't see the purpose of that function.. I mean... I don't really get it, but I can see that it works, and I do like that you came back months (or years? how long ya been here?) later to share code with someone that might need it.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Thanks for the star. It's been about 6 months or so.

I'll try to elaborate why I (personally) prefer the way ASP's server.mapPath() works over expandPath().

Lets say you have a page upload an image. This page is in Expandpath() obviously will return something along the lines of c:\webspace\ if you wanted to save the file to c:\webspace\ you would have to drop two levels to get there. i.e. expandPath("..\..\images"). The path will always change depending on the depth of the folder containing the template being "run".

Server.mappath() starts at the root so it doesn't matter where the current template is. If you want to save uploaded images to c:\webspace\ server.mapPath("images\") is all you need. it doesn't matter if your template is on the root or 10 folders deep.

To make a short story long expandpath() uses the relative path from the current template. mappath() uses the actual path from the root.

Is mapPath better than expandPath? probably not, but i like the fact you don't have to know where the template is relative to the target.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
I like the way expandpath works, but I'm used to it.. and it is something with pecuiliarities I had to get used to, so while I'll stick with it.. I do get how mapPath has its own uses.

And now, I've forgotten the next statement, its been a long day.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Actually, you CAN start at the web root with ExpandPath():

Code:
#ExpandPath("\images")#

The "\" returns the absolute path starting at the root of the web site.

-Tek
 
Thanks tek, but tell that to CF.

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

CFError said:
Parameter 1 of function ExpandPath which is now "\Images" must be a relative path. Relative paths cannot be empty strings. Relative paths cannot start with a forward slash (/), backslash (\), or a drive name, e.g., "C:".


Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
HA!
Our SA's moved CFMX to a different server and put 5 back on. so I guess that error is invalid. I tried it on an MX server and I didn't get an error but it mapped to the wrong path, not even the right drive letter.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Well that's strange, because it works on my end, CFMX 6.1 w/updater.

-Tek
 
Farther down in the docs it does mention all of the path types you can use, including relative and absolute. I assume you, bombboy, are the one who commented at the bottom of the page about the error.

So, you are using CFMX 6.1 and NOT getting an error, but if the wrong drive is being returned then something else must be up. Are you using virtual directories off a web root or just using a web root with no virtual directories? What path SHOULD be returned and what path IS being returned?

-Tek
 
Yeah that was me, i've tried to contact them to remove it. I'm sure they'll contact me anyway, or post a reply, hopefully just delete it and save me some face, hehe.

Boy do i feel lame now. See what lack of communication can do? Oh well, as i've said before I'm only human and eating my pride has never caused indegestion (it's usualy a learning experience anyway). :)

I'm not sure if the server here is using virtual directories or not, I could probably ask the SAs. MY server IS using 1 virtual directory, soon to be 2 with my new project. I don't believe my website is associated to the currently existing virtual... but the server does have them yes.

both servers return "C:\\coldfusionMX\ or something to that effect. Neither of the web roots are on c:
I can't remember the entire path to my site, it's several folders deep, but it's on e:
The other one i cant tell you, sorry.

Thanks, and sorry.


Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top