SoFunction
Updated on 2024-07-16

Difference between relative and absolute paths

Absolute path:

In our usual use of computers to find the files we need to know the location of the file, and the location of the file is expressed in the path, for example, as long as we see this path: c:/website/img/ we know that the file is in the website directory of the c disk in the subdirectory of the img. A path that completely describes the location of a file like this is an absolute path. We do not need to know any other information to determine the location of the file based on the absolute path. And in the website similar to http:///img/The way to determine the location of a file is also an absolute path..

In website applications, we usually use "/" to indicate the root directory, and /img/ means that the file is in the img directory on the root of the website. But this is risky for beginners because it is important to know that the root directory referred to here is not the root directory of your website, but the root directory of the server on which your website is hosted, and therefore an error will occur if the root directory of the website is different from the root directory of the server.
Relative Path:

Analyze why it happens that images are not displayed properly. As an example, there is a page with an image linked to it. Their absolute paths are as follows:
c:/website/
c:/website/img/

If you use the absolute path c:/website/img/, then everything will be fine on your own computer because you can indeed find the file in the specified location, i.e. c:/website/img/, but when you upload the page to the website you will probably get an error because your website may be on the server's c drive, or on the d drive, or in the aa directory, or more likely in the bb directory. There is no reason for a path like c:/website/img/ to exist. So, what kind of path should be used to locate the file in the file? Yes, it should be a relative path, the so-called relative path, as the name suggests, is relative to the target location. In the above example, you can use img/ to locate the file, then no matter where these files are placed, as long as their relative relationship has not changed, there will be no error.

In addition, we use "... /" to indicate a higher level directory, "... /... /" to indicate a higher level directory, and so on. (This may be easier to understand if you have studied dos)

Look at a few more examples, and note that in all of them there is an image linked in the file.

Example:
c:/website/web/
c:/website/img/
How should the middle link be represented in this example?
Wrong way to write: img/
This is incorrect; in this case, the absolute path represented by img/ for the file is: c:/website/web/img/, which clearly does not meet the requirements.
The correct way to write it: use ... /img/ to locate the file.


Example:
c:/website/web/xz/
c:/website/img/images/
How should the middle link be represented in this example?
Wrong way to write it: ... /img/images/
This writing is incorrect, in this case for the file ... /img/images/ represents the absolute path: c:/website/web/img/images/.
Correct writing: you can use ... /... /img/images/ to locate the file.


Example:
c:/website/web/xz/
c:/website/web/img/
How should the middle link be represented in this example?
Wrong way to write:... /... /img/
This writing is incorrect, in this case for the file... /... /img/ represents the absolute path: c:/website/img/.
The correct way to write it: you can use the relative path of . /img/ to locate the file.


Summary: Through the above examples can be found, in the absolute path into a relative path, the two files in the absolute path of the same part can be ignored, do not take into account. As long as they consider the differences can be.