1

I'm trying to create an Intranet page that looks up all pdf documents in a UNC path and the returns them in a list as hyperlinks that opens in a new window. I'm nearly there however the following code displays the FULL UNC path - My question how can I display only the Filename (preferably without the .pdf extension too). I've experimented with the basename function but can't seem to get the right result.

//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>"; 
}

The links work fine it just the display text shows //myserver/adirectory/personnel/document.pdf rather than just document. Note the above code was taken from another example I found whilst researching. If there's a whole new better way then I'm open to suggestions.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
SuperSub
  • 87
  • 1
  • 2
  • 7
  • 3
    You are looking for [`basename`](http://php.net/basename). – Jon Apr 25 '12 at 09:05
  • 1
    possible duplicate of [How to get file name from full path with PHP?](http://stackoverflow.com/questions/1418193/how-to-get-file-name-from-full-path-with-php) – Joshua Dwire Sep 04 '15 at 18:10

3 Answers3

4
echo basename($file);

http://php.net/basename

deceze
  • 510,633
  • 85
  • 743
  • 889
2

Modify your code like this:

<?
$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>"; 
}

?>
elo
  • 615
  • 4
  • 11
0

You may try this, if basename() does not work for some reason:

$file_a = explode('/',$file);
if (trim(end($file_a)) == '')
    $filename = $file_a[count($file_a)-2];
else
    $filename = end($file_a);
beerwin
  • 9,813
  • 6
  • 42
  • 57