// use dirname to get the directory of the current file
$path = dirname(__FILE__);
// $path here is now /path_to/your_dir
// split directory into array of pieces
$pieces = explode(DIRECTORY_SEPARATOR, $path);
// $pieces = ['path_to', 'your_dir']
// get the last piece
echo $pieces[count($pieces) - 1];
// result is: your_dir
9条答案
按热度按时间jaxagkaj1#
或
或(PHP5)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
您可以使用
basename()
来获取路径的尾部:)在您的情况下,我会说您最有可能使用
getcwd()
,当您有一个需要包含另一个库的文件并且该文件包含在另一个库中时,dirname(__FILE__)
会更有用。例如:
在
common.php
中,您需要使用editor.php
中的函数,因此使用common.php
:main.php
:这样,当common.php是
main.php
中的require'd
时,在common.php
中调用require_once
将正确地包括images/editor.php
中的editor.php
,而不是试图在当前运行main.php
的目录中查找。igsr9ssn2#
仅获取脚本执行所在目录的名称:
np8igboo3#
可以使用
dirname(__FILE__)
获取当前文件目录的路径。示例:
/path_to/your_dir/your_file.php
:nnvyjq4y4#
i5desfxk5#
其实我发现最好的解决办法是这样的:
如果您目录是www\var\路径*当前路径*
则返回当前路径
mdfafbf16#
第一个月
库文件/图像/index.php
结果:图像
toiithl67#
要获取当前目录的名称,我们可以使用
getcwd()
或dirname(__FILE__)
,但getcwd()
和dirname(__FILE__)
不是同义词。它们的作用与它们的名称完全相同。如果您的代码是通过引用其他目录中的另一个文件中的类来运行的,那么这两个方法将返回不同的结果。例如,如果我正在调用一个类,从那里调用这两个函数,并且这个类存在于
/index.php
的某个/controller/goodclass.php
中,那么**getcwd()
将返回'/
,dirname(__FILE__)
将返回/controller
**。9cbw7uwe8#
我使用这一行来获取实际的文件目录名,而不带路径。(适用于Windows)
我对它做了一点修改,以便也能在Linux上工作
kdfy810k9#
基本名称(getcwd());//只返回当前工作目录名。