Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Server Paths and Constants

I don't quite understand Relative Paths or Absolute Paths

This is confusing me way to much, Could some please explain in more detail.

Thanks!

2 Answers

The easiest way is to ask yourself the question "What is the location of the file that I want to link to? Is it on my server or is it on somebody's else server?".

An absolute link(path) is used when the file is on the web, for example "http://somebody's-else.com/image-gallery/img01.png"

A relative link is used when the file is on your server. Then you have to ask yourself the question "At what level is that file relative to the folder, in which is my working file? Is it on the same level, is it one level below or is it one level up?"

For example, let's imagine that you have the following structure:

  • - htdocs
  • --- (folder)img_files
  • --- (folder)x_project
  • --- index.php

So, htdocs is your root folder and index.php is your working file. If you want to make a relative path to an image in "img_files" folder, that it will look like that:

<img src="img_files/img01.png"/> 
``` (one level below current)

If for example your index.php is in the "x_project" folder, that the image location is one level up from the x_project. The path will look like that:

<img src="../img_files/img01.png"/>

".." means "one level up from the current directory"

If for example the directory looks like that:

*    - htdocs
*    --- img01.png
*    --- index.php

then the image is on the same level as your working file. The path will look like that:

<img src="img01.png"/>

It's best practice to use relative paths whenever you can but if you want to link something to a file that is on the web, then you have no choice and you have to use absolute path.

Hope this helps. :)

Thank you so much!