I have a php page inside a folder on my website.
我的网站上有一个文件夹,里面有一个php页面。
I need to add the name of the current directory into a variable for example:
我需要将当前目录的名称添加到一个变量中,例如:
$myVar = current_directory_name;
Is this possible?
这是可能的吗?
6 个解决方案
#1
184
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.dirname.php
You can use basename()
to get the trailing part of the path :)
可以使用basename()获取路径的末尾部分:)
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.
在您的例子中,我想说您最有可能使用getcwd(),当您有一个需要包含另一个库并包含在另一个库中的文件时,dirname(__FILE__)会更有用。
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
在你的共同之处。需要在编辑器中使用函数。php,所以你使用
common.php:
common.php:
require_once dirname(__FILE__) . '/images/editor.php';
main.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。php, require_once的调用。php将正确地包含编辑器。php图像/编辑器。php而不是在当前目录中查找main。运行php。
#2
12
For EXAMPLE
例如
Your Path = /home/serverID_name/www/your_route_Dir/
你的路径= /home/serverID_name / www / your_route_Dir /
THIS_is_the_DIR_I_Want
THIS_is_the_DIR_I_Want
A Soultion that WORKS:
一个工作的情况:
$url = dirname(\__FILE__);
$array = explode('\\\',$url);
$count = count($array);
echo $array[$count-1];
#3
9
echo basename(__DIR__); will return the current directory name only
echo basename(__FILE__); will return the current file name only
#4
8
Actually I found the best solution is the following:
我发现最好的解决办法是:
$cur_dir = explode('\\', getcwd());
echo $cur_dir[count($cur_dir)-1];
if your dir is www\var\path\ Current_Path
如果您的dir是www\var\path\ Current_Path
then this returns Current_path
然后它返回Current_path
#5
8
To get only the name of the directory where script executed:
只获取脚本执行的目录的名称:
//Path to script: /data/html/cars/index.php
echo basename(dirname(__FILE__)); //"cars"
#6
2
$myVar = str_replace('/','',$_SERVER[REQUEST_URI]);
$ myVar =(“/”、“大小写不敏感,$ _SERVER[REQUEST_URI]);
libs/images/index.php result: images
/ libs /图像索引。php结果:图片
#1
184
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.dirname.php
You can use basename()
to get the trailing part of the path :)
可以使用basename()获取路径的末尾部分:)
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.
在您的例子中,我想说您最有可能使用getcwd(),当您有一个需要包含另一个库并包含在另一个库中的文件时,dirname(__FILE__)会更有用。
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
在你的共同之处。需要在编辑器中使用函数。php,所以你使用
common.php:
common.php:
require_once dirname(__FILE__) . '/images/editor.php';
main.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。php, require_once的调用。php将正确地包含编辑器。php图像/编辑器。php而不是在当前目录中查找main。运行php。
#2
12
For EXAMPLE
例如
Your Path = /home/serverID_name/www/your_route_Dir/
你的路径= /home/serverID_name / www / your_route_Dir /
THIS_is_the_DIR_I_Want
THIS_is_the_DIR_I_Want
A Soultion that WORKS:
一个工作的情况:
$url = dirname(\__FILE__);
$array = explode('\\\',$url);
$count = count($array);
echo $array[$count-1];
#3
9
echo basename(__DIR__); will return the current directory name only
echo basename(__FILE__); will return the current file name only
#4
8
Actually I found the best solution is the following:
我发现最好的解决办法是:
$cur_dir = explode('\\', getcwd());
echo $cur_dir[count($cur_dir)-1];
if your dir is www\var\path\ Current_Path
如果您的dir是www\var\path\ Current_Path
then this returns Current_path
然后它返回Current_path
#5
8
To get only the name of the directory where script executed:
只获取脚本执行的目录的名称:
//Path to script: /data/html/cars/index.php
echo basename(dirname(__FILE__)); //"cars"
#6
2
$myVar = str_replace('/','',$_SERVER[REQUEST_URI]);
$ myVar =(“/”、“大小写不敏感,$ _SERVER[REQUEST_URI]);
libs/images/index.php result: images
/ libs /图像索引。php结果:图片