简体   繁体   English

PHP获取祖父dir路径

[英]PHP get grandfather dir path

I know PHP > 5.3 added the Magic constants : __DIR__ to get the father dir path, 我知道PHP> 5.3添加了Magic常量: __DIR__来获取父目录路径,

But how can I get the grandfather dir path in a smarter way? 但是,我怎样才能以更智能的方式获得祖父的路径?

1. __DIR__.'/../' 1. __DIR__.'/../'

2. dirname(__DIR__) 2. dirname(__DIR__)

Use the second parameter levels for the dirname call 使用dirname调用的第二个参数levels

echo dirname(__DIR__,2); // 1 Level up = Father. 2 Levels up = Grandfather

However, it is not any better than what you already have. 但是,它并不比你已经拥有的更好。 It's just smarter like you asked for :) 它就像你要求的那样更聪明:)

You can simply move up a folder by using .. . 您可以使用..简单地向上移动文件夹。

For example: 例如:

$path = dirname(__DIR__);
$grandfatherPath = $path.'/../../';

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

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