When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Friday, December 17, 1999

Using Server.MapPath
By Scott Mitchell


Each file on a Web server has two ways of being accessed - through a virtual path and through a physical path. The virtual path is the path one would enter into their browser's Address bar, for example:

http://www.yourserver.com/someDirectory/someFile.asp

The bold part of the URL above is referred to as the virtual path. The physical path is the actual drive, directory, and filename of a particular file on the Web server. For example, on your computer, if you run Windows 9X, your Win.ini file's physical path is C:\WINDOWS\WIN.INI (assuming you installed Windows on your C: drive).

You can obtain the virtual path of a particular ASP page by using the Request.ServerVariables collection.

Response.Write Request.ServerVariables("PATH_INFO")

will output the virtual path of the ASP page it is entered into. There is no item in the ServerVariables collection that will yield the physical path of the current ASP page. (Note that there is an item that indicates the physical root directory.) So, how do we get the physical path of an ASP page given the virtual path? Furthermore, why would we want the physical path?

At times, it is essential that we have the physical path of a particular file or directory. For example, when usign the FileSystemObject, you often need the physical path. (Curious on learning more about the FileSystemObject? Visit our FileSystemObject FAQ!)

So the problem arises, "How do we translate a virtual path into a physical path?" The answer is to use Server.MapPath, a function that takes one argument, a virtual path, and returns the corresponding physical path. Server.MapPath can be used in a number of ways. First, you can use it to obtain the physical path of a specific ASP page. Imagnie that you wanted to obtain the physical path of a specific ASP page, whose virtual path was /SomeDir/SomePage.asp. You could just do:

Response.Write Server.MapPath("/SomeDir/SomePage.asp")

The output would depend on the web site's physical root directory, but is might be something like: C:\INetpub\wwwroot\SomeDir\SomePage.asp. If you wanted to obtain the physical path of the executed ASP page, you could use the PATH_INFO ServerVariable in conjunction with Server.MapPath, like so:

Response.Write Server.MapPath(Request.ServerVariables("PATH_INFO"))

You can also use Server.MapPath to obtain the physical path of a particular directory. For example, the following two lines of code will return the physical path for the web site's root directory and for a particular directory:

'Physical path of the root directory
Response.Write Server.MapPath("/")

'Physical path of SomeDirectory
Response.Write Server.MapPath("/SomeDirectory/")

Server.MapPath is indifferent on whether you use backslashes (\) or forward slashes (/). If you do not put a forward or backward slash at the beginning of the string passed into Server.MapPath, the current directory that the ASP page is being executed is used as the base for the physical path. For example, if you had an ASP script running in the /Inc directory, which had the physical path C:\InetPub\wwwroot\Inc, the following two lines:

Response.Write Server.MapPath("somefile.txt") & "<BR>"
Response.Write Server.MapPath("/somefile.txt")

Would produce different output. The first line would use the current directory's physical path as the path for somefile.txt, and would output C:\InetPub\wwwroot\Inc\somefile.txt. The second line of code, due to the beginning forward slash, would use the root physical path, producing C:\InetPub\wwwroot\somefile.txt as the output.

That's about it! You'll find that in ASP scripts that use the FileSystemObject, Server.MapPath is used quite often.

Happy Programming!

  • By Scott Mitchell



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article