2012-04-25 34 views
0

我正在嘗試創建一個Intranet頁面,該頁面查找UNC路徑中的所有pdf文檔,並將它們作爲在新窗口中打開的超鏈接返回列表。我快到了,但是下面的代碼顯示了完整的UNC路徑 - 我的問題如何顯示只有文件名(最好不帶.pdf擴展名)。我已經嘗試了基本名稱函數,但似乎無法得到正確的結果。PHP僅提取UNC路徑中的文件名

//path to Network Share 
$uncpath = "//myserver/adirectory/personnel/"; 
//get all files with a .pdf extension. 
$files = glob($uncpath . "*.pdf"); 
//print each file name 
foreach ($files as $file) 
{ 
echo "<a target=_blank href='File:///$file'>$file</a><br>"; 
} 

的鏈接正常工作,它只是顯示文本顯示//myserver/adirectory/personnel/document.pdf而不僅僅是document。請注意,上述代碼取自我在研究中發現的另一個示例。如果有更好的方法,那麼我會接受建議。

+3

您正在尋找爲['basename'](http://php.net/basename)。 – Jon 2012-04-25 09:05:51

+1

[如何從PHP的完整路徑獲取文件名?](http://stackoverflow.com/questions/1418193/how-to-get-file-name-from-full-path-with-php) – 2015-09-04 18:10:48

回答

2

修改這樣的代碼:

<? 
$uncpath = "//myserver/adirectory/personnel/"; 
//get all files with a .pdf extension. 
$files = glob($uncpath . "*.pdf"); 
//print each file name 
foreach ($files as $file) 
{ 
echo "<a target=_blank href='File:///$file'>".basename($file)."</a><br>"; 
} 

?> 
+0

乾杯幹活!感謝所有的迅速反應。 – SuperSub 2012-04-25 09:25:18

+0

刪除擴展名: echo「".preg_replace("/\\.[^.\\s]{3,4}$/", "",basename($file))."
」; – elo 2012-04-25 09:28:09

+0

更好,非常感謝你的建議! – SuperSub 2012-04-25 09:34:11

0

你可以試試這個,如果基本名稱()不會出於某種原因:

$file_a = explode('/',$file); 
if (trim(end($file_a)) == '') 
    $filename = $file_a[count($file_a)-2]; 
else 
    $filename = end($file_a);