如何使用PHP从完整路径获取文件名?
例如,如何获取如何使用PHP从完整路径获取文件名?,php,filenames,Php,Filenames,例如,如何获取Output.map 来自 F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map 使用PHP?您正在寻找的 PHP手册中的示例如下: <?php $path = "/home/httpd/html/index.php"; $file = basename($path); // $file is set to "index.php" $file = basename($p
Output.map
来自
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
使用PHP?您正在寻找的
PHP手册中的示例如下:
<?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"
?>
该函数应能满足您的需求:
给定一个字符串,该字符串包含指向
文件,此函数将返回
文件的基本名称
例如,引用手册的页面:
<?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"
?>
您将获得:
string(10) "Output.map"
您可以使用该函数。要从URI获取确切的文件名,我将使用以下方法:
<?php
$file1 =basename("http://localhost/eFEIS/agency_application_form.php?formid=1&task=edit") ;
//basename($_SERVER['REQUEST_URI']); // Or use this to get the URI dynamically.
echo $basename = substr($file1, 0, strpos($file1, '?'));
?>
basename()在处理中文等亚洲字符时存在错误
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
我已经使用函数
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);
?>
$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
从PHP5.2.0开始就可以使用此函数
然后,您可以根据需要操纵所有零件。例如,要使用完整路径,可以执行以下操作:
<?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);
?>
$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
使用SplFileInfo: SplFileInfo SplFileInfo类提供高级面向对象的 单个文件的信息接口 参考: o/p:字符串(7)“foo.txt” 试试这个:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
很简单。例如:
<?php
function filePath($filePath)
{
$fileParts = pathinfo($filePath);
if (!isset($fileParts['filename']))
{
$fileParts['filename'] = substr($fileParts['basename'], 0, strrpos($fileParts['basename'], '.'));
}
return $fileParts;
}
$filePath = filePath('/www/htdocs/index.html');
print_r($filePath);
?>
有几种方法可以获取文件名和扩展名。您可以使用下面一个简单易用的
$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
为了在最少的行中实现这一点,我建议使用内置的
DIRECTORY\u分隔符
常量和explode(delimiter,string)
将路径分隔为多个部分,然后简单地去掉所提供数组中的最后一个元素
例如:
$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'
basename()在处理像中文这样的亚洲字符时有一个bug。谢谢Sun,由于我的应用程序将在海外使用,您刚刚为我节省了数小时的bug清除时间。我建议pathinfo
overbasename
,正如Metafaniel在下面发布的那样pathinfo()
将为您提供一个包含部分路径的数组。或者对于这里的情况,您可以明确地要求输入文件名。因此pathinfo('/var/www/html/index.php',pathinfo_FILENAME)
应该返回'index.php'
@OnethingSimple再次检查文档。。。尽管这与直觉相反,但您需要PATHINFO\u BASENAME
获取完整的index.php
PATHINFO\u FILENAME
将为您提供index
。在任何情况下,对于unicode兼容的方法,mb\u substr($filepath,mb\u strrpos($filepath,'/',0,'UTF-16LE'),NULL,'UTF-16LE')
只需将UTF-16LE替换为文件系统使用的任何字符集(NTFS和ExFAT使用UTF16)@peter Mortensen感谢您的支持请描述您更改了什么以及为什么更改,以帮助其他人识别问题并理解此答案我不认为这是一个错误,在its中提到:Caution basename()是区域设置感知的,因此它可以通过多字节字符路径查看正确的basename,必须使用setlocale()函数设置匹配的区域设置代码>。但我也更喜欢使用preg_replace,因为不同操作系统的目录分隔符不同。在Ubuntu上,``不是一个directoy分隔符,basename对它没有影响。
Array
(
[dirname] => /www/htdocs
[basename] => index.html
[extension] => html
[filename] => index
)
$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
$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'
<?php
$windows = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
/* str_replace(find, replace, string, count) */
$unix = str_replace("\\", "/", $windows);
print_r(pathinfo($unix, PATHINFO_BASENAME));
?>
$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);