例如,我如何获得Output.map
从
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
用PHP?
你正在寻找basename
.
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"
?>
我已经使用该函数完成了此操作,该函数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);
?>
这将返回:
/usr/admin/config
test.xml
xml
test
从 PHP 5.2.0 开始就可以使用这个函数了!
然后,您可以根据需要操作所有部分。例如,要使用完整路径,您可以这样做:
$fullPath = $xmlFile['dirname'] . '/' . $xmlFile['basename'];
有几种方法可以获取文件名和扩展名。您可以使用以下易于使用的。
$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
使用SplFileInfo:
SplFileInfo SplFileInfo 类为单个文件的信息提供了一个面向对象的高级接口。
参考: http: //php.net/manual/en/splfileinfo.getfilename.php
$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());
o / p:字符串(7)“foo.txt”
该basename
功能应该给你你想要的:
给定一个包含文件路径的字符串,此函数将返回文件的基本名称。
例如,引用手册页:
<?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"
?>
或者,在您的情况下:
$full = 'F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map';
var_dump(basename($full));
你会得到:
string(10) "Output.map"
尝试这个:
echo basename($_SERVER["SCRIPT_FILENAME"], '.php')
basename() 在处理像中文这样的亚洲字符时有一个错误。
我用这个:
function get_basename($filename)
{
return preg_replace('/^.+[\\\\\\/]/', '', $filename);
}
$filename = basename($path);
您可以使用basename()函数。
为了在最少的行中做到这一点,我建议使用内置DIRECTORY_SEPARATOR
常量以及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'
要从 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, '?'));
?>
$image_path = "F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map";
$arr = explode('\\',$image_path);
$name = end($arr);
这很简单。例如:
<?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);
?>
输出将是:
Array
(
[dirname] => /www/htdocs
[basename] => index.html
[extension] => html
[filename] => index
)
<?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));
?>
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/Rfxd0P"></iframe>