RELATIVE VS ABSOLUTE PATHNAMES
Files are stored on a filing system which presumably has a directory structure into which files are stored. When a master document is loaded into a browser it has to be referenced so that the browser can load the required file. This might mean giving a complete, or the absolute, pathname to the file.
Having loaded the master document then subsequent documents can be referenced using just their file names – provided they are all stored in the same sub-directory as the master document. This is called a relative link.
Hypertext referencing is equivalent to using any file store operating system. Once you have moved into a current working directory then files can referenced by their names. But files in other sub-directories have to be referenced via their absolute pathnames.
In general, it is best to set related files so that they are stored in the same sub-directory so that relative file naming can be used – its quicker!
It’s exactly the same as with the real life directions. Given the absolute address, a postal one, like “7119 W Sunset Blvd West Hollywood, CA 90046” you can find the location from anywhere. However, given the relative directions, like “keep three blocks this way and then and turn to the right” would work from the current location only, otherwise sending you astray.
So it goes for the paths in the computer world: given the absolute address, you can always get to the place, no matter from where you started. Whereas relative path is tricky, and should be used with caution, only when you positively know where you are at the moment.
On Windows, the filesystem doesn’t have the common root for the whole system but split between disks, so an absolute paths starts from the drive letter. Whereas each disk has its own root, which is a backslash – \
. So you can type cd \
and get to the root of the current disk.
So you can tell that windows is rather confusing, but for the simplicity we would pretend that we have only one disk, and within its boundaries the rules are pretty much the same as in Unix.
So now you can tell an absolute path from a relative one – it is starting from the root, which is:
- on a Unix file system it’s
/
- on a web serer it’s again
/
- on Windows it’s either
\
(for the current disk) orD:\
(system-wide)
Relative paths
If you don’t supply the root, it means that your path is relative.
The simplest example of relative path is just a file name, like index.html
. So one should be careful with relative paths. If your current directory is /about/
then index.html
would be one, but if you switch it to /contacts/
then it will be another.
Other relative path examples:
./file.php
(the file is in the current folder. The same as justfile.php
)images/picture.jpg
(the file is in the images folder that is in the current directory)../file.php
(file is in the folder that is one level higher than the current directory)../../file.php
(file is in the folder that is two levels higher than the current directory)