文档根 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11927968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Document Root PHP
提问by Andrew
Just to confirm, is using:
只是为了确认,正在使用:
$_SERVER["DOCUMENT_ROOT"]
the same as using: /
与使用相同:/
in HTML.
在 HTML 中。
Eg. If current document is:
例如。如果当前文档是:
folder/folder/folder/index.php
I could use (in HTML) to start at the roort:
我可以使用(在 HTML 中)从根开始:
/somedoc.html
and to do the same in PHP I would have to use:
并在 PHP 中做同样的事情,我将不得不使用:
$_SERVER["DOCUMENT_ROOT"] . "/somedoc.html";
Is that correct? Is there an easier way to do it?
那是对的吗?有没有更简单的方法来做到这一点?
回答by khaverim
<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/
$_SERVER['DOCUMENT_ROOT'] 呈现一个实际的文件路径(在我的计算机上作为它自己的服务器运行,c:/wamp/www/
HTML's / renders the root of the server url, in my case, localhost/
HTML 的 / 呈现服务器 url 的根目录,在我的例子中是 localhost/
But c:/wamp/www/hello.html and localhost/hello.html are in fact the same file
但是 c:/wamp/www/hello.html 和 localhost/hello.html 实际上是同一个文件
回答by sachleen
Just /refers to the root of your website from the public html folder. DOCUMENT_ROOTrefers to the local path to the folder on the server that contains your website.
只是/从公共 html 文件夹中引用您网站的根目录。DOCUMENT_ROOT是指服务器上包含您的网站的文件夹的本地路径。
For example, I have EasyPHP setup on a machine...
例如,我在一台机器上安装了 EasyPHP ......
$_SERVER["DOCUMENT_ROOT"]gives me file:///C:/Program%20Files%20(x86)/EasyPHP-5.3.9/wwwbut any file I link to with just /will be relative to my wwwfolder.
$_SERVER["DOCUMENT_ROOT"]给了我,file:///C:/Program%20Files%20(x86)/EasyPHP-5.3.9/www但我链接到的任何文件/都将与我的www文件夹相关。
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 /.
如果您想提供服务器上文件的绝对路径(从服务器的根目录),您可以使用DOCUMENT_ROOT. 如果您想从网站的根目录提供文件的绝对路径,请仅使用/.
回答by timeSmith
Yes, on the server side $_SERVER['DOCUMENT_ROOT']is equivalent to /on the client side.
是的,在服务器端$_SERVER['DOCUMENT_ROOT']相当于/在客户端。
For example: the value of "{$_SERVER['DOCUMENT_ROOT']}/images/thumbnail.png"will be the string /var/www/html/images/thumbnail.pngon 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
例如: 的值"{$_SERVER['DOCUMENT_ROOT']}/images/thumbnail.png"将是/var/www/html/images/thumbnail.png服务器上的字符串,可以从客户端的 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/
不,换句话说,它的值$_SERVER['DOCUMENT_ROOT']不是/服务器的本地路径,它指向服务器向客户端显示的内容example.com/
note: $_SERVER['DOCUMENT_ROOT']does not include a trailing /
注意:$_SERVER['DOCUMENT_ROOT']不包括尾随/
回答by Adam F
The Easiest way to do it is to have good site structure and write it as a constant.
最简单的方法是拥有良好的站点结构并将其编写为常量。
DEFINE("BACK_ROOT","/var/www/");

