Relative Path, absolute path

Source: Internet
Author: User

Absolute path:
We all know that when we use a computer, we need to find the desired file, and we must know the location of the file. The path is used to indicate the location of the file. For example, if we see this path: C: /website/img/photo.jpgwe know that the photo.jpg file is in the IMG subdirectory under the website directory of drive C. The path similar to this complete description file location is an absolute path. We can determine the file location based on the absolute path without knowing any other information. The formula for determining the file location in a website similar to http://www.pckings.net/img/photo.jpgis also absolute.

In addition, in the application of the website, we usually use "/" to represent the root directory./img/photo.jpgthen the photo.jpg file is in the IMG directory on the root directory of the website. However, this is risky for beginners, because you need to know that the root directory here is not the root directory of your website, but the root directory of the server where your website is located, therefore, an error occurs when the root directory of the website is different from the root directory of the server.


Relative Path:
Let's analyze why the image cannot be normally displayed. Now there is a page index.htm, which is connected with an image photo.jpg. Their absolute path is as follows:
C:/website/index.htm
C:/website/img/photo.jpg

If you use the absolute path C:/website/img/photo.jpg, everything works on your computer, because it can indeed be C at the specified position: /website/img/photo.jpgto find the photo.jpg file, but when you upload the page to the website, it is very likely that an error will occur, because your website may be on the drive C of the server, or disk D, it may also be in the AA directory, or in the BB directory. In short, there is no reason for a path such as C:/website/img/photo.jpg. In the index.htm file, how can I use the example to locate the photo.jpg file? Yes, it should be a relative path. The so-called relative path, as its name suggests, is its own relative location to the target. In the previous example, you can use img/photo.jpg to locate the file attached to index.htm. no matter where these files are stored, as long as their relative relationship remains unchanged, no errors will occur.

In addition, we use "../" to represent the upper-level directory, "http://www.cnblogs.com/##upload the upper-level directory, in this way. (Friends who have learned DoS may be easier to understand)

See several examples. All examples are attached with an image photo.jpg in the index.htmfile.

Example:
C:/website/web/index.htm
C:/website/img/photo.jpg
In this example, how should photo.jpg connected to index.htmbe be represented?
Incorrect syntax: img/photo.jpg
In this example, IMG/photo.jpg represents the absolute path C:/website/web/img/photo.jpg for the index.htm file, which obviously does not meet the requirements.
Correct syntax: Use the relative path of ../img/photo.jpg to locate the file

Example:
C:/website/web/xz/index.htm
C:/website/img/images/photo.jpg
In this example, how should photo.jpg connected to index.htmbe be represented?
Incorrect syntax: ../img/images/photo.jpg
In this example, the absolute path for the index.htm file ../img/images/photo.jpg is: C:/website/web/img/images/photo.jpg.
Correct syntax: You can use the relative location of http://www.cnblogs.com/img/images/photo.jpgto locate the file.

Example:
C:/website/web/xz/index.htm
C:/website/web/img/photo.jpg
In this example, how should photo.jpg connected to index.htmbe be represented?
Incorrect writing: http://www.cnblogs.com/img/photo.jpg
This write method is optional. For the index.htm file in this example, http://www.cnblogs.com/img/photo.jpgstands for the absolute representation of the file at: c://website/img/photo

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.