Absolute vs. Relative Paths

November 30, 2008

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