[英]How do I get a file name from a full path with 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
函數完成了此操作,該函數創建了一個包含路徑部分的數組供您使用! 例如,您可以這樣做:
<?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: 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);
}
$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, '?'));
?>
基本名稱不適用於我。 我從表單(文件)獲得文件名。 在Google Chrome(Mac OS X v10.7(Lion))中,文件變量變為:
c:\fakepath\file.txt
當我使用時:
basename($_GET['file'])
它返回:
c:\fakepath\file.txt
因此,在這種情況下,孫俊文的答案最有效。
在Firefox上,文件的變量不包含此falsepath。
$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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.