How do i go back to the root directory in php?

Consider this project structure:

/path/to/projectroot/index.php
                     header.php
                     include/inc.php

If index.php had

include('include/inc.php');

and inc.php had

include('header.php');

You'd get that error since the line in inc.php would be looking for

/path/to/projectroot/include/header.php  (doesn't exist)

not

/path/to/projectroot/header.php (does exist)

There are a few ways people resolve this.

1: Absolute paths

The first, and most straightforward is to use absolute paths.

If index.php had

include('include/inc.php');

and inc.php had

include('/path/to/projectroot/header.php');

This would work.

2: Absolute paths with defines

Similar to #1, if index.php had

define('PROJECT_ROOT', '/path/to/projectroot/');
include(PROJECT_ROOT.'include/inc.php');

and inc.php had

include(PROJECT_ROOT.'header.php');

This would work.

Update: As noted in the comments by pichan, you could use one of the "magic" constants here in index.php, so:

index.php

define('PROJECT_ROOT', __DIR__.'/');
include(PROJECT_ROOT.'include/inc.php');

and inc.php

include(PROJECT_ROOT.'header.php');

Note we add a trailing slash to __DIR__ here since:

This directory name does not have a trailing slash unless it is the root directory.

3: Include both and hide errors

If inc.php had

@include('header.php');    # Try this directory
@include('../header.php'); # Try parent directory

This would work.[1]

4: Assume current directory unless otherwise specified

If index.php had

$rel_prefix_to_root = '../';
include('include/inc.php');

and inc.php had

if(!isset($rel_path_to_root)){ $rel_path_to_root = ''; }
include($rel_path_to_root . 'header.php');

This would work.

My take on these methods

1 and 2 are basically the same, but 2 is a little bit easier and more common for big projects since it allows you to make one constant definition and use it site-wide. It also allows you to deploy the project on multiple servers (placed in multiple paths) and only requires changing one line project-wide, as opposed to one line in each file for option 1.

3 is terrible, don't do it. Sometimes you'll see it, you might even see it in tutorials online. Don't do it.

4 should probably be avoided in favor of 1 or 2. But this approach might be necessary if you have some complex set of includes.

Some Notes:

[1] This is a terrible idea. It works, but don't do it.

Posting back, maybe this will help someone else.

It seems that part of my confusion is that there isn't one solution for specifying the root path for both HTML paths and PHP paths. At least I can't make one work.

For HTML:
A preceding slash '/' does get you to the root for image paths for HTML.

I must have forgotten that gem.
This however does not seem to work for PHP paths for use with INCLUDE statements.

For PHP:
$_SERVER['DOCUMENT_ROOT'] does work for includes.

However, in my particular instance it does so only on my local server. I am running my site on the Mosso Hosting cloud and $_SERVER['DOCUMENT_ROOT'] does not produce the correct server path, so I have to specify it manually.

$_SERVER['DOCUMENT_ROOT'] does not work for me if echo'd as part of the image tag HREF path.

C.

  1. HowTo
  2. PHP Howtos
  3. Find Root Directory Path in PHP

Created: October-06, 2021 | Updated: March-21, 2022

  1. Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHP
  2. Use the dirname() Function to Find the Path of the Root Directory of a Project in PHP
  3. Use $_SERVER['DOCUMENT_ROOT'] to Find the Document Root Directory of a File in PHP

We will introduce different methods to find the path of the root directory of a PHP project.

Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHP

In PHP, there are predefined constants that can be used to achieve various functionalities. __DIR__ is one magical constant that returns the complete file path of the current file from the root directory. It means it will return the file’s directory. dirname(__FILE__) can also be used for the same purpose.

Suppose we have a project folder which is the root directory of the project. The project folder has the following file path /var/www/HTML/project. Inside the project folder, we have the index.php file and another folder master. Inside the master folder, we have two PHP files: login.php and register.php.

project
├── index.php
└── master
 ├── login.php
 └── register.php

Suppose we are currently working on login.php. In such a file structure, we can get the directory’s path using the __DIR__ constant in the login.php file. We can use the echo function to print the constant.

Example Code:


Output:

/var/www/html/project/master

Use the dirname() Function to Find the Path of the Root Directory of a Project in PHP

The function dirname(__FILE__) is similar to __DIR__. We can find the path of the directory of a file using this function. We can also move to the upper levels in the file path using the dirname() function. The first parameter of the function is the path of the file, which is denoted by the __FILE__ constant. The second parameter is an integer which is called levels. We can set the levels to direct the function to level up in the file path. The default value of the level is 1. As we increase the level, the function will get the file path of one level up. So, we can use this function to find the exact file path of the project’s root directory in PHP.

For example, we can consider the file structure as the first method. Working from the file, login.php, we can use the dirname() function with level 2 and the __FILE__ constant as parameters. Then we can get the exact file path of the working directory. Thus, we can change the levels according to our choice to move upward and downward in the file path. In this way, we can find the path of the root directory of the project in PHP.

Example Code:


Output:

/var/www/html/project

Use $_SERVER['DOCUMENT_ROOT'] to Find the Document Root Directory of a File in PHP

We can use the $_SERVER[] array with the DOCUMENT_ROOT indices to find the document root directory of the currently executing script. It will return the complete path of the document root directory. It is defined in the configuration file in the server. For the file structure above, we can print the $_SERVER['DOCUMENT_ROOT'] with the echo function to find the document root directory of the file login.php.

As shown in the output below, we found out the path html is the document root directory of the login.php file. We can see the file path of the root directory as well.

Example Code:


Output:

/var/www/html

Related Article - PHP File

  • Write Into a File in PHP
  • Require_once vs Include in PHP
  • Create and Get the Path of tmpfile in PHP
  • Remove PHP Extension With .Htacess File
  • How do I get to the root directory in PHP?

    Find Root Directory Path in PHP.
    Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHP..
    Use the dirname() Function to Find the Path of the Root Directory of a Project in PHP..
    Use $_SERVER['DOCUMENT_ROOT'] to Find the Document Root Directory of a File in PHP..

    How do I get to the root directory?

    To locate the system root directory: Press and hold the Windows key, then press the letter 'R'. (On Windows 7, you can also click start->run… to get the same dialog box.) Enter the word “cmd” in the program prompt, as shown, and press OK.

    How do I get the current directory back in PHP?

    If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.

    Where is my site root directory?

    The web root is the folder where the website files for a site are stored. Each site under your host gets an unique root folder. The root folder is placed under the sites username. Inside the web root folder you'll find folders like \logs , \public , \private and more.

    What does __ DIR __ return?

    When you reference the __DIR__ inside a file, it returns the directory of the file. The __DIR__ doesn't include a trailing slash e.g., / or \ except it's a root directory. When you use the __DIR__ inside an include, the __DIR__ returns the directory of the included file.

    How do I get the root path in html?

    Just put / as the first character of your link and it will point to the root directory of the website.