Document Root PHP
<a href="<?php echo $_SERVER['DOCUMENT_ROOT'].'/hello.html'; ?>">go with php</a>
<br />
<a href="/hello.html">go to with html</a>
Try this yourself and find that they are not exactly the same.
$_SERVER['DOCUMENT_ROOT']
renders an actual file path (on my computer running as it's own server, C:/wamp/www/
HTML's /
renders the root of the server url, in my case, localhost/
But C:/wamp/www/hello.html
and localhost/hello.html
are in fact the same file
Server Document Root Path in PHP
$files = glob($_SERVER["DOCUMENT_ROOT"]."/myFolder/*");
PHP $_SERVER['DOCUMENT_ROOT']
$_SERVER['DOCUMENT_ROOT'] returns
The document root directory under which the current script is executing, as defined in the server's configuration file.
You could use $_SERVER['HTTP_HOST']
or absolute paths like <a href="/nmc/Admin/LoginPage.php">Login</a>
How to get a file that's one folder above Document Root?
Use ../
to up one directory.
include($_SERVER['DOCUMENT_ROOT'] . '/../Controllers/MyFile.php');
How to reference to a folder that is above Document Root, in PHP?
You can use directly:
include('/home/myusername/private/config.php');
$_SERVER document root in CLI
$_SERVER contains headers which won't be available in the CLI. The web server defines the document root. In the CLI, you aren't using a web server, so there is no document root.
You can try to rely on environmental variables, assuming they are set by your shell.
For instance, PWD
represents the current directory and HOME
represents the user's home directory.
$pwd = getenv('PWD');
$home = getenv('HOME');
You can also use __FILE__
or __DIR__
magic constants to try and depict the path you are currently in.
What does it mean In case the PHP file is outside the document root, only its directory is scanned?
Just to clarify a couple of things in addition to Julian's answer.
The default php.ini file for the server (e.g. /path/to/default/ini/php.ini
) will always be scanned, no matter where the PHP file itself is located. What happens after that depends on whether the PHP file is stored in the document root path or not.
In Document Root Path
Say you have a PHP file saved at /path/to/doc/root/another/dir/file.php
.
The server will look for an ini file in the following directories:
/path/to/root/another/dir
/path/to/root/another
/path/to/root
If an ini file is found in any of those locations, they will be scanned as well as the default ini file.
Outside Document Root Path
Say you have a PHP file saved at /path/outside/doc/root/file.php
.
The server will look in the following directory:
/path/outside/doc/root
If an ini file is found in this location (i.e. /path/outside/doc/root/php.ini
), it will be scanned as well as the default ini file.
Related Topics
PHP Datetime Microseconds Always Returns 0
Www to Non-Www Redirect with PHP
Can't Execute PHP Script Using PHP Exec
Change Name of Laravel's Created_At and Updated_At
PHP Float Calculation Error When Subtracting
How to Check If Variable Is Array... or Something Array-Like
Add Class="Active" to Active Page Using PHP
Google API Client "Refresh Token Must Be Passed in or Set as Part of Setaccesstoken"
MySQL Join Multiple Rows as Columns
Error_Reporting(E_All) Does Not Produce an Error
PHP Day of Week Numeric to Day of Week Text
How to Loop Through a Multidimensional Array Without Knowing It's Depth
Object of Class Stdclass Could Not Be Converted to String
Walk Array Recursively and Print the Path of the Walk
I'm Getting This Error Code: Invalid Argument Supplied for Foreach()