Absolute Path is the hierarchical path that allows you to locate a file or folder in a file system from the root. The absolute path of a file allows you to specify the exact location of the file, regardless of the location of the user’s current directory.
Links are fundamental elements of the World Wide Web, especially when they are presented as absolute paths in computer terms.
Indeed, absolute paths give a clear idea of the location of a file to which they are attached. So
- What are they?
- Are they important?
- And how can you tell the difference between an absolute path and a relative path?
If you would like to know more about the concept of absolute paths, I invite you to continue reading this article.
Chapter 1: What does Absolute Path really mean?
Let’s discover in this chapter the real meaning of the term Absolute Path and the clarification of some related terms.
1.1) How do we define Absolute Path?
In simple terms, Absolute Path is an address consisting of a complete list of the location of a file, folder or directory on a computer.
The absolute path includes the complete location of the file or folder, including the drive on which it is located.
For example, on a Windows computer, the absolute path to a file might be:
C:\Windows\Users\Billy\docs\lesson1.txt
Server systems need to know the absolute path of every file they access in order to locate it. Each different operating system, such as Windows, Linux, MacOS, uses a different syntax or way to write an absolute path.
There is also a term called ”relative Path”, which refers to the location of a file relative to the current location. For example, the file above in Windows might be listed as “\docs#1.txt”.
The only time you need to know and use absolute paths in WordPress is when you are a developer and need to reference the exact location of a file when writing code
To find the absolute path of files used for WordPress, you can write three simple lines of code in PHP and store this information on your web server to use as a reference.
1.2. some terminology to know
In order to better understand the difference between Absolute Path and similar terms, we will clarify a few in this section.
1.2.1. Document Root
The document root, also known as the web root or web site root directory, is the publicly accessible base folder for a web site. This is where you will find the index file :
- Index.php
- Index.html
- Default.html
- Etc.
This folder is often named public_html, htdocs, www or wwwroot, depending on your host and your settings.
The document root is a directory or folder that is stored on your host’s servers and is designated to contain web pages. When someone else views your website, this is the location they will access.
For a website to be accessible to visitors, it must be published in the correct directory, that is, the root of the document.
For example, if you have a website www.mywebsite.com with an index.html file at the root of the document, the navigation to this web address will serve this index file.
1.2.2. Server Root or web server paths
As it was said before, for the browser, there are no files on the server
A user of the site never has access to the server file system. And for the browser, there is only one site root which is always a single slash.
Therefore, to make an HTML link absolute, you would simply have to create it from the root of the site.
Let’s say your site has two sections, http://www.example.com/about/info.php and http://www.example.com/job/vacancy.php, while you want to redirect ”vacancy.php” to the ”info.php”
If you do it as is , your browser will certainly not find it.
Especially remember that the browser always tries to create the link to the full address, using the current location, which is /about/. This means that the resulting path /about/vacancy.php will be wrong
So, for this to work properly, you must necessarily make this link absolute, starting from the root of the site :/job/vacancy.php.
The same goes for all the internal links of the site, images, js and css files, hyperlinks or any other resource that can be clicked or loaded on the page.
For local resources, it is preferable to make it an absolute path only, without any protocol or domain such as /job/vacancy.php
On the other hand, for external resources, these attributes are mandatory, so it should be a complete URL like http://www.example.com/job/vacancy.php.
1.3. What is the difference between an Absolut Path and a Relative Path?
To emphasize the difference between these two terms, we will discuss each of them to make things clear.
3.1. notion of Relative Path
A relative path refers to a location that is relative to a current directory.
Source : Code-boxxx
Relative paths use two special symbols: a dot (.) and the double dotted symbol (..), which designate the current directory and the parent directory
The double dotted line is used to move up in the hierarchy. A single dot represents the current directory.
In the example directory structure given below, let’s assume that you used Windows Explorer to access D:\Data\Shapefiles\Soils
Once in this directory, a relative path will use D:\Data\Shapefiles\Soils as the current directory, until you access a new directory to make it the current directory
The current directory is sometimes referred to as the root directory.
Here’s how to locate a relative path:
3.1.1. File references in the same directory
If the source file and the reference file are in the same directory, simply write the name of the reference file directly, or use ./ to indicate the current directory.
Assuming that the path info.html is: c:/sites/blabla/info.html and that the path index.html is: c:/sites/blabla/index.html.
In info.html, the index.html file must be referenced as a hyperlink:
3.1.2 File references in the top-level directory
Here is what these symbols mean:
- …/ indicates the top-level directory of the directory where the source file is located;
- …/…/indicates the top-level directory of the directory where the source file is located, and so on. as follows:
3.1.3 File references in subordinate directories
To reference a file in a subordinate directory, simply write the path to the file in the subordinate directory
Assuming that the path to info.html is c:/Inetpub/wwwroot/sites/blabla/info.html and the path to index.html is c:/Inetpub/wwwroot/sites/blabla/html/index.html, add index into info.html.
The .html hyperlink code should be written like this:
3.2. Notion of absolute path
The absolute path starts from the root directory to the directory you are in
Thus, using “/” indicates the directory as c:/site/img/photo.jpg
The path similar to this full description file location is the absolute path.
3.3. Advantages and disadvantages of absolute and relative paths
The advantages and disadvantages of relative paths are almost the opposite of absolute paths
As advantages of absolute paths:
- If someone plagiarizes your site’s content, the links inside will point to your website. This would only really be possible if the person does not remove the links from their content. Similarly, if someone saves your web page on a local computer, the links, images, css and js will still connect to your website;
- If the location of the web page changes, the links inside still point to the correct URL.
And as disadvantages of absolute paths:
- Absolute paths cannot be used when creating a test application, because the link must point to the domain name instead of the test application;
- The content page must be fixed. If all content pages use absolute paths, then if you want to modify a file, the path on the other pages cannot be changed and it will point to the original address.
Here are the advantages of relative paths:
- The content can be easily moved as well as the whole directory;
- The test method is more flexible.
And as disadvantages of relative paths:
- When the content page changes its position, the link easily fails;
- It is easy to be plagiarized.
3.4. The point of convergence between an absolute path and a relative path
The difference between these two terms can be summarized in just two important points:
- If the path is built from the system root, it is said to be absolute;
- If the path is built from its current location, then it is relative. This makes sense, because it is relative to our current location.
With the absolute address, you can always get to any destination, no matter where you started from
The relative path, on the other hand, is tricky and should only be used when you know exactly where you are now.
Chapter 2: How do I find an absolute path?
Here are some ways to find absolute paths to files stored on your devices:
2.1. How to find the absolute path in Windows
To find the absolute path of a file stored on your Windows PC, you just have to make a right click on the file and then click on the option ”Properties”.
You will find, at this stage, the exact path that leads to the file, this is translated by ”Location”.
We can note that the location of the file is C:\Users\UTLISATEUR\Pictures.
It is thus advisable to add a backslash followed by the name of the file to make the path absolute.
As result it will become ”C:\Users\UTLISATEUR\Pictures\chrome.jpg”.
In addition, you can also use the command line windows to recover the absolute path of a file on your computer.
Let us suppose that you have a directory ”C:\Windows\” and that you would like to find the absolute path code.bj. You just have to fill in ‘C:\Windows\code.bj, that would mean that the absolute path of this file is the directory plus the file name.
2.2. how to find the absolute path of a file under Linux
Instead of the Windows command line, we talk here about the pwd command. This allows you to quickly find the absolute path of a file in your device.
As with the Windows command line, the process of finding the absolute path remains the same in Linux.
You must add to the address of the current directory the name of the file you are looking for.
Conclusion
In general, we can note that absolute paths describe exactly the final destination of a file in a hierarchical order.
This is a good way, because it makes it easier to find the file on a device.
Unlike relative paths, absolute paths can lead you to a directory in which a web page of your site is stored, for example.
In this article we have tried to provide some tips for finding absolute paths to a file.
If you have other ways to find the absolute paths of a file, either on windows or linux, don’t hesitate to share them with us in the comments.
See you soon!