Server. mappath

Source: Internet
Author: User

./Current Directory
/Website home directory
../Upper directory
~ /Website virtual directory
If the current website directory is E: \ wwwroot, the web page path browsed by E: \ wwwroot \ company is E: \ wwwroot \ company \ news \ show. asp
Use
Server. MapPath ("./") Return Path: E: \ wwwroot \ company \ news
Server. MapPath ("/") Return Path: E: \ wwwroot
Server. MapPath ("../") Return Path: E: \ wwwroot \ company
Server. MapPath ("~ /") Return Path: E: \ wwwroot \ company
Server. MapPath (request. ServerVariables ("Path_Info "))
Request. ServerVariables ("Path_Translated ")
The preceding two methods return the path D: \ wwwroot \ company \ news \ show. asp.

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Syntax
Server. MapPath (Path)

Parameters
Path
Specifies the relative or virtual path of the physical directory to be mapped. If the Path starts with a forward slash (/) or backslash (\), The MapPath method regards the Path as a complete virtual Path when returning the Path. If the Path does not start with a slash, The MapPath method returns the Path relative to the existing Path in the. asp file.
Note
The MapPath method does not support relative path syntax (.) or (..). For example, the following relative path ../MyDir/MyFile.txt returns an error.
The MapPath method does not check whether the returned path is correct or whether it exists on the server.
Because the MapPath method only maps paths regardless of whether the specified directory exists, you can first map the paths of the physical directory structure with the MapPath method, it is then passed to the component that creates the specified directory or file on the server.
Example
For the following example, the data.txt file and the test. asp file containing the following scripts are all in the C: \ Inetpub \ Wwwroot \ Script directory. The C: \ Inetpub \ Wwwroot directory is set as the host directory of the server.
The following example uses the server variable PATH_INFO to map the physical path of the current file. Script
<% = Server. mappath (Request. ServerVariables ("PATH_INFO") %> <BR>

Output
C: \ inetpub \ wwwroot \ script \ test. asp <BR>

Because the path parameters in the following example do not start with a slash, they are mapped to the current directory, where C: \ Inetpub \ Wwwroot \ Script. Script
<% = Server. mappath ("data.txt") %> <BR>
<% = Server. mappath ("script/data.txt") %> <BR>

Output
C: \ inetpub \ wwwroot \ script \ data.txt <BR>
C: \ inetpub \ wwwroot \ script \ data.txt <BR>

The following two examples use the slash character to specify the full virtual path of the returned path on the server. Script
<% = Server. mappath ("/script/data.txt") %> <BR>
<% = Server. mappath ("\ script") %> <BR>

Output
C: \ inetpub \ script \ data.txt <BR>
C: \ inetpub \ script <BR>

The following example shows how to use a forward slash (/) or backslash (\) to return the physical path of the home directory. Script
<% = Server. mappath ("/") %> <BR>
<% = Server. mappath ("\") %> <BR>

Output
C: \ inetpub \ wwwroot <BR>
C: \ inetpub \ wwwroot <BR>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.