简体   繁体   English

PHP动态获取根目录绝对路径

[英]PHP obtain root dir absolute path dynamically

I'm a newbie of php. 我是php的新手。

I would like to create a php config file where to set the absolute path of my project directory structure. 我想创建一个php配置文件,用于设置项目目录结构的绝对路径。 My config file is located in 'private/class' directory and it will be included in 'root' and private directory files. 我的配置文件位于“ private / class”目录中,它将包含在“ root”和私有目录文件中。

Project directory structure is 项目目录结构为

class
img
photo
etc
private/class
private/photo
private/photo/thumb
private/etc

To get the absolute path where the file is located, I use magic php costants DIR , 为了获得文件所在的绝对路径,我使用了神奇的php costants DIR

define('MAINPATH', __DIR__ );

this show 这个节目

/var/www/projectname/private/class

and this variable remains the same if I include the config file in the index.php or private/index.php , etc. 如果我将配置文件包含在index.php或private / index.php等中,则此变量保持不变。

Now I have the problem of finding the other path starting from the main one. 现在,我遇到了从主路径开始寻找另一条路径的问题。 Usually I use realpath, but in this case I do not think will go well. 通常,我使用realpath,但是在这种情况下,我认为效果不会很好。 I tried in this way 我以这种方式尝试

define('PHOTOPATH', __DIR__ . '../photo');

define('PHOTOPATH', realpath ( __DIR__ . '../photo'));

these show 这些节目

1 - /var/www/projectname/private/class../photo
2 - nothing

How could I do this? 我该怎么办? Thank you 谢谢

You just need to append the PATH SEPARATOR ( / ) before appending the rest of the path. 您只需要在添加路径的其余部分之前添加PATH分隔符( / )。

define('PHOTOPATH', realpath ( __DIR__ . '/../photo'));
                                         ^
                                         |
                                         +- notice here the '/' is added

您可以使用$ _SERVER ['DOCUMENT_ROOT']。

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

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