例如,我如何得到output。map
from
F:\程序文件\SSH通信安全\SSH安全Shell\Output.map
使用PHP吗?
例如,我如何得到output。map
from
F:\程序文件\SSH通信安全\SSH安全Shell\Output.map
使用PHP吗?
当前回答
有几种方法可以获取文件名和扩展名。你可以用下面这个简单易用的。
$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
其他回答
您可以使用basename()函数。
我已经使用函数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'];
SplFileInfo:
SplFileInfo类提供了一个高级的面向对象 到单个文件的信息的接口。
裁判:http://php.net/manual/en/splfileinfo.getfilename.php
$info = new SplFileInfo('/path/to/foo.txt');
var_dump($info->getFilename());
或/ p:管柱(7)“foo。txt”
$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
)