如何使用 PHP 从完整路径中获取文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1418193/
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
How do I get a file name from a full path with PHP?
提问by omg
For example, how do I get Output.map
例如,我如何获得 Output.map
from
从
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
with PHP?
用PHP?
回答by Mark Rushakoff
回答by Metafaniel
I've done this using the function PATHINFOwhich creates an array with the parts of the path for you to use! For example, you can do this:
我已经使用函数PATHINFO创建了一个包含路径部分的数组供您使用!例如,您可以这样做:
<?php
$xmlFile = pathinfo('/usr/admin/config/test.xml');
function filePathParts($arg1) {
echo $arg1['dirname'], "\n";
echo $arg1['basename'], "\n";
echo $arg1['extension'], "\n";
echo $arg1['filename'], "\n";
}
filePathParts($xmlFile);
?>
This will return:
这将返回:
/usr/admin/config
test.xml
xml
test
The use of this function has been available since PHP 5.2.0!
从PHP 5.2.0开始就可以使用这个功能了!
Then you can manipulate all the parts as you need. For example, to use the full path, you can do this:
然后您可以根据需要操作所有部件。例如,要使用完整路径,您可以这样做:
$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
回答by Pascal MARTIN
The basenamefunction should give you what you want:
该basename功能应该给你你想要的:
Given a string containing a path to a file, this function will return the base name of the file.
给定一个包含文件路径的字符串,此函数将返回文件的基本名称。
For instance, quoting the manual's page:
例如,引用手册页:
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>
Or, in your case:
或者,就您而言:
$full = 'F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map';
var_dump(basename($full));
You'll get:
你会得到:
string(10) "Output.map"
回答by Khadka Pushpendra
There are several ways to get the file name and extension. You can use the following one which is easy to use.
有多种方法可以获取文件名和扩展名。您可以使用以下易于使用的方法。
$url = 'http://www.nepaltraveldoor.com/images/trekking/nepal/annapurna-region/Annapurna-region-trekking.jpg';
$file = file_get_contents($url); // To get file
$name = basename($url); // To get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // To get extension
$name2 =pathinfo($url, PATHINFO_FILENAME); // File name without extension
回答by internals-in
With SplFileInfo:
使用SplFileInfo:
SplFileInfo The SplFileInfo class offers a high-level object oriented interface to information for an individual file.
SplFileInfo SplFileInfo 类为单个文件的信息提供了一个面向对象的高级接口。
Ref: http://php.net/manual/en/splfileinfo.getfilename.php
参考:http: //php.net/manual/en/splfileinfo.getfilename.php
$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());
o/p: string(7) "foo.txt"
o/p: string(7) "foo.txt"
回答by atwebceo
Try this:
尝试这个:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
回答by p4bl0
$filename = basename($path);
回答by Sun Junwen
basename() has a bug when processing Asian characters like Chinese.
basename() 在处理像中文这样的亚洲字符时有一个错误。
I use this:
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\/]/', '', $filename);
}
回答by Vertigo
You can use the basename()function.
您可以使用basename()函数。
回答by Douglas Tober
To do this in the fewest lines I would suggest using the built-in DIRECTORY_SEPARATORconstant along with explode(delimiter, string)to separate the path into parts and then simply pluck off the last element in the provided array.
要在最少的行中做到这一点,我建议使用内置DIRECTORY_SEPARATOR常量以及explode(delimiter, string)将路径分成几部分,然后简单地取出提供的数组中的最后一个元素。
Example:
例子:
$path = 'F:\Program Files\SSH Communications Security\SSH SecureShell\Output.map'
//Get filename from path
$pathArr = explode(DIRECTORY_SEPARATOR, $path);
$filename = end($pathArr);
echo $filename;
>> 'Output.map'

