从目录名 (__FILE__) 向上获取 2 级

发布于 2024-11-17 11:40:40 字数 191 浏览 2 评论 0原文

如何从当前文件(仅 2 个目录以上)返回路径名?

因此,如果我当前的文件 URL 返回 theme/includes/functions.php

我怎样才能返回“theme/”

当前我正在使用

return dirname(__FILE__)

How can I return the pathname from the current file, only 2 directories up?

So if I my current file URL is returning theme/includes/functions.php

How can I return "theme/"

Currently I am using

return dirname(__FILE__)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忆依然 2024-11-24 11:40:40

PHP 5.3+

return dirname(__DIR__);

PHP 5.2 及更低版本

return dirname(dirname(__FILE__));

对于 PHP7,通过指定 dirname 的第二个参数来进一步向上移动目录树。 7 之前的版本将需要进一步嵌套 dirname

http://php.net/manual/en/function.dirname.php

PHP 5.3+

return dirname(__DIR__);

PHP 5.2 and lower

return dirname(dirname(__FILE__));

With PHP7 go further up the directory tree by specifying the 2nd argument to dirname. Versions prior to 7 will require further nesting of dirname.

http://php.net/manual/en/function.dirname.php

心碎的声音 2024-11-24 11:40:40

dirname(dirname(__FILE__)); 更简单的是使用 __DIR__

dirname(__DIR__);

从 php 5.3 开始工作。

Even simpler than dirname(dirname(__FILE__)); is using __DIR__

dirname(__DIR__);

which works from php 5.3 on.

渡你暖光 2024-11-24 11:40:40
[ web root ]
    / config.php
    [ admin ] 
        [ profile ] 
            / somefile.php 

如何将 config.php 包含在 somefile.php 中?您需要使用当前 somefile.php 文件中具有 3 个目录结构的 dirname。

require_once dirname(dirname(dirname(__FILE__))) . '/config.php'; 

dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file
[ 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.

require_once dirname(dirname(dirname(__FILE__))) . '/config.php'; 

dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file
莫多说 2024-11-24 11:40:40

正如 @geo 所建议的,这是一个增强的目录名函数,它接受具有目录名搜索深度的第二个参数:

/**
 * 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;
}

注意:@todo 可能是相关的。

唯一的问题是,如果此函数位于外部包含文件中(例如 util.php),则无法使用它来包含此类文件:B

As suggested by @geo, here's an enhanced dirname function that accepts a 2nd param with the depth of a dirname search:

/**
 * 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.

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

醉生梦死 2024-11-24 11:40:40

虽然迟到了,但您也可以执行如下操作,根据需要多次使用 \..\..\ 来向上移动目录级别。

$credentials = 需要 __DIR__ 。 '\..\App\Database\config\file.php';

相当于:

$credentials = dirname(__DIR__) 。 '\App\Database\config\file.php';

的好处是它避免了必须嵌套目录名,例如:

dirname(dirname(dirname(__DIR__))

请注意,这在 IIS 服务器上进行了测试 - 不确定 Linux 服务器,但我不明白为什么它不起作用。

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';

Which is the equivalent to:

$credentials = dirname(__DIR__) . '\App\Database\config\file.php';

The benefit being is that it avoids having to nest dirname like:

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.

你是暖光i 2024-11-24 11:40:40
require_once(dirname(__FILE__) . "/../../functions.php");
require_once(dirname(__FILE__) . "/../../functions.php");
输什么也不输骨气 2024-11-24 11:40:40

这是一个老问题,但仍然相关。

使用:

basename(dirname(__DIR__));

仅返回第二个父文件夹名称 - 在本例中为“主题”。

http://php.net/manual/en/function.basename.php

This is an old question but sill relevant.

Use:

basename(dirname(__DIR__));

to return just the second parent folder name - "theme" in this case.

http://php.net/manual/en/function.basename.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文