I have this code which is taking the name of files in a directory and then creating a link to those files. However, the text in the link has the file extension on the end. I want to remove this but at the same time keep the correct link to the file i.e. the HTML link needs the extension to remain on it – like this:
<a href="/documents/other/file.pdf">File</a>
So here is my script:
$linkdir="documents/other";
$dir=opendir("documents/other");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php")
{
array_push($files, $file);
}
}
natcasesort($files);
$files=array_reverse($files);
foreach ($files as $file)
print "<li><a href='/$linkdir/$file' rel='external'>$file</a></li>";
Here is the code I need to integrate to remove the file extension:
$name = substr($file, 0, strrpos($file, '.'));
Any help with this will be greatly appreciated.
Could you mean that you want this?
I can’t find any other question in your post 🙂