获取当前文件 PHP 的父文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11164459/
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
Get parent folder of current file PHP
提问by Andrey
I've done quite a bit of searching for this, so I'm sorry if this is a dupe.
Anyway, I need to get the name of the folder the current file is in. For example, I want to convert something like example.com/folder/subfolder/file.phpto subfolder.
My current code is dirname($_SERVER['PHP_SELF']), but that returns /folder/subfolderinstead of subfolder. Thank you!
我已经做了很多搜索,所以如果这是一个骗局,我很抱歉。
无论如何,我需要获取当前文件所在文件夹的名称。例如,我想将类似的内容转换example.com/folder/subfolder/file.php为subfolder.
我当前的代码是dirname($_SERVER['PHP_SELF']),但它返回/folder/subfolder而不是subfolder。谢谢!
回答by Alfonso de la Osa
The simpliest way is:
最简单的方法是:
basename(__DIR__);
回答by DaveRandom
You need to combine your existing code using dirname()with a call to basename():
您需要将现有代码 usingdirname()与调用结合起来basename():
$parent = basename(dirname($_SERVER['PHP_SELF']));
回答by Luke Pittman
dirname()used with basename()would work ... also this if you want to get them all:
dirname()使用 withbasename()会起作用......如果你想把它们全部都用这个:
$folders = explode ('/', $_SERVER['PHP_SELF']);
Now $folders would contain an array of all of the folder names.
现在 $folders 将包含所有文件夹名称的数组。
Cheers.
干杯。
回答by tribulant
$folder = basename(dirname(__FILE__));
回答by MK2009
str_replace('/','',$_SERVER[REQUEST_URI]);
This works for php7: str_replace('/','',$_SERVER["REQUEST_URI"]);
这适用于 php7: str_replace('/','',$_SERVER["REQUEST_URI"]);
Result:
结果:
lib/**images**/index.php
lib/**images**/index.php
images
图片

