Just to confirm, is using:
$_SERVER["DOCUMENT_ROOT"]
the same as using: /
in HTML.
Eg. If current document is:
folder/folder/folder/index.php
I could use (in HTML) to start at the roort:
/somedoc.html
and to do the same in PHP I would have to use:
$_SERVER["DOCUMENT_ROOT"] . "/somedoc.html";
Is that correct? Is there an easier way to do it?
php //PHP Script to find Document Root of Application $docroot = getenv("DOCUMENT_ROOT"); echo $docroot; ?> As per above PHP script actual DOCUMENT ROOT of application will be stored in $docroot variable which can be used further in application.
The folder/directory on a Web server that contains the Web pages visible to the public. Also called the "docroot," the folder names are often /www/public or /public_html.
The web server document root is the root directory of the web server running on your system. The documents under this root are accessible to any system connected to the web server (provided the user has permissions). If a file is not under this root directory, then it cannot be accessed through the web server.
$_SERVER['DOCUMENT_ROOT'] returns. The document root directory under which the current script is executing, as defined in the server's configuration file.
<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
Just /
refers to the root of your website from the public html folder. DOCUMENT_ROOT
refers to the local path to the folder on the server that contains your website.
For example, I have EasyPHP setup on a machine...
$_SERVER["DOCUMENT_ROOT"]
gives me file:///C:/Program%20Files%20(x86)/EasyPHP-5.3.9/www
but any file I link to with just /
will be relative to my www
folder.
If you want to give the absolute path to a file on your server (from the server's root) you can use DOCUMENT_ROOT
. if you want to give the absolute path to a file from your website's root, use just /
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With