35

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?

Andrew
  • 489
  • 1
  • 7
  • 13

4 Answers4

46
<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

khaverim
  • 3,386
  • 5
  • 36
  • 46
5

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

sachleen
  • 30,730
  • 8
  • 78
  • 73
2

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 /

Martin
  • 22,212
  • 11
  • 70
  • 132
timeSmith
  • 553
  • 1
  • 8
  • 16
  • "on the server side $_SERVER['DOCUMENT_ROOT'] is equivalent to / on the client side." this is ambiguous at best and potentially misleading. `$_SERVER['DOCUMENT_ROOT']` should never be used as a reference address in HTML. Always simply use HTML `/` . – Martin Jul 23 '21 at 20:31
  • I have moved the clarification statement to be part of the initial statement, and cleaned up the result, in an attempt to mitigate the ambiguity you (https://stackoverflow.com/users/3536236/martin) saw. – timeSmith Jul 27 '21 at 16:54
-4

The Easiest way to do it is to have good site structure and write it as a constant.

DEFINE("BACK_ROOT","/var/www/");
Adam Fowler
  • 1,750
  • 1
  • 17
  • 18
  • 1
    i know its old but i stumbled upon this, why would you make more constants if you already have a php defined server constant doing this, waste of code, cpu and memory – DarkMukke Nov 04 '13 at 10:35
  • The idea is, no matter how far deep (folder-wise) you are in includes, this makes it so you'll never have to write code relative to the script, other than the code used to define the root. I use this to combat the Jerry-rigging method used by many programmers today. – Adam Fowler Nov 05 '13 at 03:37
  • 12
    yes but that way you script becomes depended on the system or on the location, which it should never be, apps should be portable – DarkMukke Nov 06 '13 at 14:56