Document Root PHP

后端 未结 4 954
时光说笑
时光说笑 2020-12-03 04:39

Just to confirm, is using:

$_SERVER[\"DOCUMENT_ROOT\"]

the same as using: /

in HTML.

Eg. If current document is:

         


        
相关标签:
4条回答
  • 2020-12-03 05:12
    <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

    0 讨论(0)
  • 2020-12-03 05:19

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

    0 讨论(0)
  • 2020-12-03 05:19

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

    DEFINE("BACK_ROOT","/var/www/");
    
    0 讨论(0)
  • 2020-12-03 05:21

    Yes, on the server side $_SERVER['DOCUMENT_ROOT'] is equivalent to / on the client side.

    For example: the value of "{$_SERVER['DOCUMENT_ROOT']}/images/thumbnail.png" will 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

    No, in other words the value of $_SERVER['DOCUMENT_ROOT'] is not / rather it is the server's local path to what the server shows the client at example.com/

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

    0 讨论(0)
提交回复
热议问题