php 从 dirname( __FILE__) 获得 2 个级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6511020/
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
Get 2 levels up from dirname( __FILE__)
提问by levi
How can I return the pathname from the current file, only 2 directories up?
如何从当前文件返回路径名,只有 2 个目录?
So if I my current file URL is returning theme/includes/functions.php
所以如果我当前的文件 URL 正在返回 theme/includes/functions.php
How can I return "theme/"
我怎样才能返回“主题/”
Currently I am using
目前我正在使用
return dirname(__FILE__)
回答by Charles Sprayberry
PHP 7
PHP 7
return dirname(__FILE__, 2);
PHP 4 and higher
PHP 4 及更高版本
return dirname(dirname(__FILE__));
With PHP7 you can get further up the directory tree by specifying the levels parameter, while pre-7 PHP will require further nesting of the dirname
function.
使用 PHP7,您可以通过指定 levels 参数进一步向上目录树,而 pre-7 PHP 将需要进一步嵌套该dirname
函数。
回答by cweiske
回答by Friede Petr
[ web root ]
/ config.php
[ admin ]
[ profile ]
/ somefile.php
How can you include config.php in somefile.php? You need to use dirname with 3 directories structure from the current somefile.php file.
如何在 somefile.php 中包含 config.php?您需要将 dirname 与当前 somefile.php 文件中的 3 个目录结构一起使用。
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file
回答by puiu
Late to the party, but you can also do something like below, using \..\..\
as many times as needed to move up directory levels.
迟到了,但您也可以执行以下操作,\..\..\
根据需要使用多次以向上移动目录级别。
$credentials = require __DIR__ . '\..\App\Database\config\file.php';
$credentials = require __DIR__ . '\..\App\Database\config\file.php';
Which is the equivalent to:
这相当于:
$credentials = dirname(__DIR__) . '\App\Database\config\file.php';
$credentials = dirname(__DIR__) . '\App\Database\config\file.php';
The benefit being is that it avoids having to nest dirname like:
好处是它避免了嵌套 dirname ,如:
dirname(dirname(dirname(__DIR__))
dirname(dirname(dirname(__DIR__))
Note, that this is tested on a IIS server - not sure about a linux server, but I don't see why it wouldn't work.
请注意,这是在 IIS 服务器上测试的 - 不确定 linux 服务器,但我不明白为什么它不起作用。
回答by Jorge Orpinel
As suggested by @geo, here's an enhanced dirname function that accepts a 2nd param with the depth of a dirname search:
正如@geo 所建议的,这是一个增强的 dirname 函数,它接受具有 dirname 搜索深度的第二个参数:
/**
* Applies dirname() multiple times.
* @author Jorge Orpinel <[email protected]>
*
* @param string $path file/directory path to beggin at
* @param number $depth number of times to apply dirname(), 2 by default
*
* @todo validate params
*/
function dirname2( $path, $depth = 2 ) {
for( $d=1 ; $d <= $depth ; $d++ )
$path = dirname( $path );
return $path;
}
Note: that @todo may be relevant.
注意:@todo 可能是相关的。
The only problem is that if this function is in an external include (e.g. util.php) you can't use it to include such file :B
唯一的问题是,如果此函数在外部包含(例如 util.php)中,则不能使用它来包含此类文件:B
回答by Rocky Kumar
require_once(dirname(__FILE__) . "/../../functions.php");
回答by codywohlers
This is an old question but sill relevant.
这是一个老问题,但仍然相关。
Use:
用:
basename(dirname(__DIR__));
to return just the second parent folder name - "theme" in this case.
只返回第二个父文件夹名称 - 在这种情况下为“主题”。