How to Include Absolute and Relative Paths in PHP

Olorunfemi Akinlua Feb 02, 2024
  1. Absolute and Relative Paths in PHP
  2. Use include() and dirname() to Include Absolute and Relative Paths in PHP
How to Include Absolute and Relative Paths in PHP

Whether on servers or local PCs, the path from which you are executing your PHP program makes a difference and is important. If you are running in the wrong directory, understanding how to use the two types of paths, relative and absolute, is important.

Though both are important, we may often use absolute paths, especially within the context of the whole PHP application rather than absolute paths.

This article will explain how to use or include absolute and relative paths within PHP.

Absolute and Relative Paths in PHP

Whether within a PHP application or our local computers, the absolute path is the file or directory location relative to the system’s root directory. However, a relative path points to a specific location in a file system relative to the current directory.

This is an absolute path on a Unix or Linux:

/home/user/devs

On a Windows PC, the absolute path looks like this:

C:\AMD\Chipset_Software

However, the Linux path system is often used for most PHP servers.

A relative path, however, can look something like this:

../devs

The above code snippet will represent the parent directory of the devs directory, and you can use the same .. on Windows. Say you are in the C:\Users\HP\Documents directory; you can go to the Downloads directory, which is a child of HP, by the below command:

cd ..\Downloads

Use include() and dirname() to Include Absolute and Relative Paths in PHP

To access the absolute and relative path, we need the dirname() function and a magical constant named __FILE__. The magical constant __FILE__ returns the full path and name of the PHP file that’s being executed.

The dirname() function returns the parent directory’s path. When we use the __FILE__ and the dirname() function together, we can obtain the absolute root path to the file we are handling, given that the file we are interacting with via an include keyword is within the same directory.

include(dirname(__FILE__).'/functions.php');

The result of the dirname(__FILE__) is:

/home/runner/ScholarlyOvalFirewall

However, suppose the PHP file we interact with is within the parent directory of the current working directory. In that case, we can use the __DIR__ constant as it returns the current file’s directory without the trailing slash.

include(dirname(__DIR__).'/functions.php');

The results of the dirname(__FILE__) is:

/home/runner/

Say, we intend to work with just relative paths, we can do so by creating the function below:

function relativepath($to){
    $a=explode("/",dirname(__FILE__));
    $index= array_search("$to",$a);
    $str="";
    for ($i = 0; $i < count($a)-$index-2; $i++) {
        $str.= "../";
    }
    return $str;
    }

$st = relativepath('main.php');
echo $st;

The function’s argument is the name of the PHP file, and it works by breaking the absolute path string into an array via the / symbol and searching for the specified PHP file. Afterward, the series of ../ is appended to the $str variable to give us the relative path to the root.

Also, we can define the root path using the dirname() function, define() function and any magical constants __DIR__ and __FILE__, depending on your need.

define('ROOT_PATH', dirname(__DIR__) . '/');

The ROOT_PATH is now /home/runner.

Olorunfemi Akinlua avatar Olorunfemi Akinlua avatar

Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.

LinkedIn