Absolute vs. Relative Paths

When referancing any external item, be it an included file in PHP, a CSS document in HTML, or an image, you have the choice of using either an absolute or relative path. An absolute path says where a file is starting from the root directory of the computer. Such paths are always correct, no matter the location of referencing (parent) file. For example, a PHP script can include a file using 

include( 'c:/php/includes/file.php');
include( '/usr/xyz/includes/file.php');

Assuming file exists in the named location, the inclusion will work (barring any permissions issues). The second example, in case you’re not familiar with the syntax, would be a Unix ( and Mac OS X) absolute path. Absolute paths always start with something like c:/ or / .

A relative path uses the referencing (parent) file as the starting point. To move up one folder, use two periods together. To move into folder, use its name followed by a slash. So assuming the current script is in the www/ex1 folder and you want to include something in www/ex2, the code will be:

include('../ex2/file.php');

A relative path will remain accurate, even if moved to another server, as long as the file keep their current relationship.

I hope this tutorial was helpful. if you any have any question don’t hesitate to ask.

Advertisements

2 Responses to Absolute vs. Relative Paths

  1. Computeryah says:

    Good 🙂
    So , the summury is that the “Absolute paths always start with something like c:/ or / ”
    and the “relative path uses the referencing (parent) file as the starting point. To move up one folder, use two periods together. To move into folder, use its name followed by a slash.”

  2. joker400 says:

    Yes man that is a good summary

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: