Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document Root PHP

Tags:

html

php

root

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?

like image 771
Andrew Avatar asked Aug 13 '12 03:08

Andrew


People also ask

Where is my PHP document root?

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.

What is the document root?

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.

What is SERVER document root?

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.

What is $_ SERVER DOCUMENT_ROOT in PHP?

$_SERVER['DOCUMENT_ROOT'] returns. The document root directory under which the current script is executing, as defined in the server's configuration file.


2 Answers

<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

like image 171
khaverim Avatar answered Sep 24 '22 15:09

khaverim


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 /.

like image 22
sachleen Avatar answered Sep 23 '22 15:09

sachleen