简体   繁体   English

如何在PHP上获取root目录

[英]How to get root dir on PHP

I use realpath('../'), it work fine but the result is D:wampwww ( real path is D://wamp/www ). 我使用realpath('../'),它工作正常,但结果是D:wampwww(真实路径是D:// wamp / www)。 Anybody can tell me how to get realpath by the right way? 任何人都可以告诉我如何以正确的方式获得真实路径? Thanks you verry much. 非常感谢你。

使用:

$_SERVER['DOCUMENT_ROOT'];

You can put some php file into the root and get: 你可以将一些php文件放入root中并获取:

$rootPath = dirname(__FILE__)

inside it. 在里面。

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. 如果在include中使用,则返回包含文件的目录。 This is equivalent to dirname(__FILE__) . 这相当于dirname(__FILE__) This directory name does not have a trailing slash unless it is the root directory. 除非它是根目录,否则此目录名称没有尾部斜杠。

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 directory where projects resides : 或者最常见的是获取项目所在的服务器文档根目录

$_SERVER['DOCUMENT_ROOT'] or filter_input(INPUT_SERVER, 'DOCUMENT_ROOT')

See : "magical" PHP constants 请参阅: “神奇的”PHP常量

如果使用php 5.3或更高版本然后使用

filter_input(INPUT_SERVER, 'DOCUMENT_ROOT');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM