PHP获取当前目录的名称 [英] PHP Get name of current directory
问题描述
我的网站上的文件夹中有一个php页面.
我需要将当前目录的名称添加到变量中,例如:
$myVar = current_directory_name;
这可能吗?
getcwd();
或
dirname(__FILE__);
或(PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
您可以使用basename()
来获取路径的结尾部分:)
就您而言,我想说您最有可能希望使用getcwd()
,当您的文件需要包含另一个库并且包含在另一个库中时,dirname(__FILE__)
更为有用.
例如:
main.php
libs/common.php
libs/images/editor.php
在您的common.php
中,您需要使用editor.php
中的功能,因此您可以使用
common.php
:
require_once dirname(__FILE__) . '/images/editor.php';
main.php
:
require_once libs/common.php
这样,当common.php在main.php
中为require'd
时,在common.php
中对require_once
的调用将正确地包含在images/editor.php
中的editor.php
,而不是尝试在当前目录中查找main.php
运行.
I have a php page inside a folder on my website.
I need to add the name of the current directory into a variable for example:
$myVar = current_directory_name;
Is this possible?
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
You can use basename()
to get the trailing part of the path :)
In your case, I'd say you are most likely looking to use getcwd()
, dirname(__FILE__)
is more useful when you have a file that needs to include another library and is included in another library.
Eg:
main.php
libs/common.php
libs/images/editor.php
In your common.php
you need to use functions in editor.php
, so you use
common.php
:
require_once dirname(__FILE__) . '/images/editor.php';
main.php
:
require_once libs/common.php
That way when common.php is require'd
in main.php
, the call of require_once
in common.php
will correctly includes editor.php
in images/editor.php
instead of trying to look in current directory where main.php
is run.
这篇关于PHP获取当前目录的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!