PHP 获取当前目录的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9997391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:03:37  来源:igfitidea点击:

PHP Get name of current directory

php

提问by Satch3000

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?

这可能吗?

回答by Andreas Wong

getcwd();

or

或者

dirname(__FILE__);

or (PHP5)

或 (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.phpyou need to use functions in editor.php, so you use

在您common.php需要在 中使用函数editor.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'din main.php, the call of require_oncein common.phpwill correctly includes editor.phpin images/editor.phpinstead of trying to look in current directory where main.phpis run.

这样,当的common.php是require'dmain.php,的通话require_oncecommon.php会包含正确editor.phpimages/editor.php,而不是试图在当前目录下查找其中main.php运行。

回答by yesnik

To get only the name of the directory where script executed:

仅获取执行脚本的目录的名称:

//Path to script: /data/html/cars/index.php
echo basename(dirname(__FILE__)); //"cars"

回答by user2169219

For EXAMPLE

例如

Your Path = /home/serverID_name/www/your_route_Dir/

Your Path = /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];

回答by Ameer Ul Islam

echo basename(__DIR__); will return the current directory name only
echo basename(__FILE__); will return the current file name only

回答by Andrew

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

如果你的目录是 www\var\path\ Current_Path

then this returns Current_path

那么这将返回Current_path

回答by MK2009

$myVar = str_replace('/', '', $_SERVER[REQUEST_URI]);

$myVar = str_replace('/', '', $_SERVER[REQUEST_URI]);

libs/images/index.php
Result: images

libs/图像/index.php
结果:图像

回答by Dziamid Harbatsevich

If you have PATH and do not want to make concern if your script works in the folder you are interested in then this work out:

如果你有 PATH 并且不想担心你的脚本是否在你感兴趣的文件夹中工作,那么这个工作:

basename(parse_url($_GET['dir_link'])['path'],PHP_URL_PATH)