Extras

Absolute VS Relative Paths
(and the Document Root)

An absolute path is a path from the system root (the topmost folder of an operating system) to another file or directory (folder). The system root is indicated on:

  • Mac/Unix by a forward slash /
  • Windows by a drive letter, colon and backslash, this is usually C:\

A relative path points to the location of a file relative to the current working directory (cwd). When writing PHP, the current working directory is the folder containing the script that is currently being processed.

If pages use an include in a different folder, when the include is running, the cwd is the folder that holds the include.

This screenshot shows the location of files running on MAMP on a Mac.

Below, you can see some examples of absolute paths that relate to the folders shown in this screenshot.

A file explorer. Inside the htdocs folder there are 3 PHP files (about.php, contact.php and index.php) plus two folders (img and includes). In the includes folder there is a header.php file

The absolute path to index.php is:
/Applications/MAMP/htdocs/index.php

When code in the index.php file is running, the cwd is:
/Applications/MAMP/htdocs/

The absolute path to header.php is:
/Applications/MAMP/htdocs/includes/header.php

When code in the header.php file is running, the cwd is:
/Applications/MAMP/htdocs/includes/

The index.php file can include header.php using the relative path: includes/header.php

In index.php, the relative path img/logo.png would point to the logo.png file in the img folder.

If a path starts with ../ it indicates that the file is up a directory from the current working directory.

In header.php, the relative path ../img/logo.png would point to the logo.png file in the img folder.


Document Root Folder

Websites have another type of root folder called the document root folder. This is a folder on the web server that maps to the domain name/hostname.

For example, when a web server hosts the domain name:
http://example.com

It could map to the following folder on the server:
/var/www/examplecom/

Every file that a browser can request must be inside the document root folder, including PHP pages, images, CSS and JavaScript files.

If you requested the URL:
http://example.com/index.php
the web server would run the file:
/var/www/examplecom/index.php.

If you requested the URL:
http://example.com/about.php,
the web server would run the file:
/var/www/examplecom/about.php.

The default document root folder for:
MAMP on Mac is /Applications/MAMP/htdocs/
XAMPP on PC is C:\XAMPP\htdocs\


Root Relative URLs

When a path that is used in a web page starts with a forward slash, the forward slash indicates the document root folder.

A root-relative path will start with a forward slash, and is followed by the path after that is relative from the document root folder.

In the folder structure shown in the screenshot above, any page could use the path /img/logo.png to refer to the logo.png file in the img folder.

You should only use root relative paths in URLs for files that the browser might request. As far as the PHP interpreter is concerned, the forward slash would refer to a different folder.

When your PHP code tries to refer to another file, you should use absolute paths.

Sites often store the path to the document root folder in a constant, then use this at the start of any paths to other files. This has two advantages:

  • It saves typing the path to the document root
  • If the path to the document root folder changes (perhaps because the site moves to another server), the path only needs updating in one place