从dirname(__FILE__)获得2级

时间:2021-07-19 13:21:14

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__)

7 个解决方案

#1


80  

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参数进一步提升目录树,而7之前的PHP将需要进一步嵌套dirname函数。

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

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

#2


39  

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

甚至比dirname(dirname(__ FILE__))简单;正在使用__DIR__

dirname(__DIR__);

which works from php 5.3 on.

哪个适用于php 5.3 on。

#3


5  

[ 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?您需要在当前somefile.php文件中使用带有3个目录结构的dirname。

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

dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file

#4


1  

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 <jorge@orpinel.com>
 * 
 * @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

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

#5


1  

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__。 '\ .. \软件\数据库\ CONFIG \ file.php';

Which is the equivalent to:

这相当于:

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

$ credentials = dirname(__ DIR__)。 '\应用\数据库\配置\ file.php';

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

好处是它避免了必须嵌套dirname,如:

dirname(dirname(dirname(__DIR__))

目录名(目录名(目录名(__ 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服务器,但我不明白为什么它不起作用。

#6


0  

require_once(dirname(__FILE__) . "/../../functions.php");

#7


0  

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

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

#1


80  

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参数进一步提升目录树,而7之前的PHP将需要进一步嵌套dirname函数。

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

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

#2


39  

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

甚至比dirname(dirname(__ FILE__))简单;正在使用__DIR__

dirname(__DIR__);

which works from php 5.3 on.

哪个适用于php 5.3 on。

#3


5  

[ 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?您需要在当前somefile.php文件中使用带有3个目录结构的dirname。

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

dirname(dirname(dirname(__FILE__))) . '/config.php'; # 3 directories up to current file

#4


1  

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 <jorge@orpinel.com>
 * 
 * @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

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

#5


1  

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__。 '\ .. \软件\数据库\ CONFIG \ file.php';

Which is the equivalent to:

这相当于:

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

$ credentials = dirname(__ DIR__)。 '\应用\数据库\配置\ file.php';

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

好处是它避免了必须嵌套dirname,如:

dirname(dirname(dirname(__DIR__))

目录名(目录名(目录名(__ 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服务器,但我不明白为什么它不起作用。

#6


0  

require_once(dirname(__FILE__) . "/../../functions.php");

#7


0  

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

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