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.
Created: October-06, 2021 | Updated: March-21, 2022 We will introduce different methods to find the path of the root directory of a PHP project.__DIR__
Predefined Constant to Find the Path of the Directory of a File in PHPdirname[]
Function to Find the
Path of the Root Directory of a Project in PHP$_SERVER['DOCUMENT_ROOT']
to Find the Document Root Directory of a File in PHP
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