【发布时间】:2010-11-27 22:33:12
【问题描述】:
例如,我如何获得Output.map
来自
F:\Program Files\SSH Communications Security\SSH Secure Shell\Output.map
使用 PHP?
【问题讨论】:
例如,我如何获得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 而不是 basename,就像 Metafaniel 在下面发布的那样。 pathinfo() 会给你一个包含路径部分的数组。或者对于这里的情况,您可以专门询问文件名。所以pathinfo('/var/www/html/index.php', PATHINFO_FILENAME) 应该返回'index.php' PHP Pathinfo documentation
PATHINFO_BASENAME 获得完整的index.php。 PATHINFO_FILENAME 会给你index。
mb_substr($filepath,mb_strrpos($filepath,'/',0,'UTF-16LE'),NULL,'UTF-16LE') - 只需将 UTF-16LE 替换为您的文件系统使用的任何字符集(NTFS 和 ExFAT 使用 UTF16)
我已经使用函数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'];
【讨论】:
\n 更改为 <br> 才能获得您所描述的格式化输出?
\n 替换为 <br>。我的代码打算使用命令行运行,因此换行符与\n 工作。问候。
有几种方法可以获取文件名和扩展名。您可以使用以下易于使用的。
$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: string(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);
}
【讨论】:
Caution basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function. 。但我也更喜欢使用 preg_replace,因为操作系统之间的目录分隔符不同。在 Ubuntu 上,`\` 不是目录分隔符,basename 对其没有影响。
$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
$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>
【讨论】:
这很简单。例如:
<?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
)
【讨论】: