如何在 PHP 上获取根目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1427721/
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
How to get root dir on PHP
提问by asedra_le
I use realpath('../'), it work fine but the result is D:wampwww ( real path is D://wamp/www ). Anybody can tell me how to get realpath by the right way? Thanks you verry much.
我使用 realpath('../'),它工作正常,但结果是 D:wampwww(真实路径是 D://wamp/www)。任何人都可以告诉我如何以正确的方式获得 realpath 吗?非常感谢你。
回答by MarcoPolo
Use:
用:
$_SERVER['DOCUMENT_ROOT'];
回答by Vladislav Rastrusny
You can put some php file into the root and get:
您可以将一些 php 文件放入根目录并获得:
$rootPath = dirname(__FILE__)
inside it.
在里面。
回答by Jyotiranjan
To get root directory path of a PHP project:
获取 PHP 项目的根目录路径:
For PHP >= 5.3.0
对于 PHP >= 5.3.0
use: __DIR__
用: __DIR__
Note:
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
注意:文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于dirname(__FILE__). 除非它是根目录,否则此目录名称没有尾部斜杠。
For PHP < 5.3.0
对于 PHP < 5.3.0
use: dirname(__FILE__)or realpath(dirname(__FILE__))
使用:dirname(__FILE__)或realpath(dirname(__FILE__))
Or in most common for getting server document root directorywhere projects resides :
或者最常见的是获取项目所在的服务器文档根目录:
$_SERVER['DOCUMENT_ROOT'] or filter_input(INPUT_SERVER, 'DOCUMENT_ROOT')
See : "magical" PHP constants
请参阅:“神奇”的 PHP 常量
回答by royalwebs
If using php 5.3 or up then use
如果使用 php 5.3 或更高版本,则使用
filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');

