Skip to main content

Absolute vs. Relative

One of the most common errors with file handling is to hard-code in a file location. Often a new programmer or web person will designate that a file can be found on their flash drive using a statement similar to this:

P:/webSite/computerCareersOLD2007/graphic/gradient/grey2blue.jpg

On that specific computer this statement will work, but what if the person puts the flash drive on another computer and it becomes drive E: or H:? The image or file will not be found.

Instead you always want to use a relative address. In this case the location of the file you are wanting is "relative" to the file that is doing the work.

For example, let's say a web page wants to use this particular gradient. Instead of hard-coding in the entire example you would give "directions" based on the location of the file.

If the file that wanted to use the gradient was in the gradient folder you could simply say:

<img src="grey2blue.jpg" alt="grey to blue gradient" />

If the file that wanted to use the gradient was in the computerCareersOLD2007 folder you would have to describe the path to the gradient, relative to where the web page was:

<img src="graphic/gradient/grey2blue.jpg" alt="grey to blue gradient" />

Each slash tells the program to look inside a sub folder.

Using relative path statements is much like giving someone directions. You give directions based on where the person is starting from. From where you are now, go straight to the third intersection and turn right...

The file is saying the same thing. From where I am located, I need to look inside a subfolder named graphic and then inside another subfolder named gradient to find the file named grey2blue.jpg