Document Root PHP

Viewed 147123

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?

4 Answers

Yes, $_SERVER['DOCUMENT_ROOT'] contains the server side path which correlates to the client side URL path /. But, No they are not interchangeable.

They are not interchangeable because, for example, the server side path should never be sent to the client (HTML) side. The value of $_SERVER['DOCUMENT_ROOT'] is not /; it is the server's local file path to what the server shows the client at /. So, the value of ${$_SERVER['DOCUMENT_ROOT']}/images/thumbnail.png" may be the string /var/www/html/images/thumbnail.png on a server where it's local file at that path can be reached from the client side at the url http://example.com/images/thumbnail.png

note: $_SERVER['DOCUMENT_ROOT'] does not include a trailing /

Related