PHP provides at least two "working paths" for your files :
- The function
getcwd()
, which returns the current working directory (it explains its name),
- The
dirname(__FILE__)
, which returns the current script's path.
You can find more informations here about getcwd
:
According to the documentation of the include
statement ( https://secure.php.net/manual/en/function.include.php ) :
The include statement includes and evaluates the specified file.
[...]
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
According to the doc, include
will use the script's directory, which is in your case your current working directory (cwd).
The situation
Now, you have the following situation :
- A file at
/meta.php
,
- A file at
/header.php
, which needs to include /meta.php
,
- A file at
/pages/page1.php
which needs to include /header.php
.
The question is : where is your working directory ?
If you run the script /pages/page1.php
, your working directory is /pages/
.
So, to access /header.php
, you must use this statement inside /pages/page1.php
:
include '../../header.php';
Or :
include $_SERVER['DOCUMENT_ROOT'] . '/header.php';
These statements are equivalents due to the path of /header.php
.
In /header.php
, the following statement will fail :
include 'meta.php';
Why ? Because your current working directory is /pages/
, so include 'meta.php';
refers to include '/pages/meta.php';
which do not exist, resulting in an error. Remember that the include
statement tries to use the current working directory if the URL is relative.
A possible solution
To access to the /meta.php
file from the /header.php
, you can either :
- Use
include '../../meta.php';
in /header.php
if you are calling your script from the /pages/page1.php
file,
- Use
include dirname(__FILE__) . '/meta.php';
in /header.php
,
- Use
include $_SERVER['DOCUMENT_ROOT'] . '/meta.php';
in header.php
.
In your case, $_SERVER['DOCUMENT_ROOT']
is probably the same that dirname(__FILE__)
, because /header.php
is at document root path. However, the former statement is only valid if header.php
path is document root. The later is better because it only depends from the path of the current file.
Which one to use ?
The second solution is probably better, because for instance the following statement in /pages/subfolder/page.php
will fail :
include '../../../header.php';
The /header.php
file will be properly included, but if it contains the include '../../meta.php';
statement, it will refers to include '/pages/meta.php';
, because your working subdirectory is /pages/subfolder/
.
It is a good design to use the dirname(__FILE__)
alternative when you want to include a script relatively from the current file, indepently from the current working directory. Just do not forget the final /
because dirname
gives the folder of the current script without it.
In conclusion
In /pages/page1.php
, use :
include '../../header.php';
In /header.php
, use :
include dirname(__FILE__) . '/meta.php';