Kez Kez - 1 year ago 111
PHP Question

how to remove file extension for display, but retaining extension for href?

I'm displaying all file from a folder, that part works fine. However, Each link displays the file extension. I have tried removing it, but seems to remove the file name too. I need to display the name of the file without extension, but use a variable with the extension for the

of the link.



$info = pathinfo($filename);
$name = $info['filename'];
$ext = $info['extension'];

$name = pathinfo($filename)['filename'];

$dir = opendir('folder/');
while ($read = readdir($dir))

if ($read!='.' && $read!='..')
echo '<li><a class="" href="folder/'.$read.'">'.$name.'</a></li>';




Any help is much appreciated.

Answer Source

easier way to do this using PHP's ScanDir

$files =  array_diff(scandir('folder/'), array('.', '..'));

foreach($files as $file){

    $name = strstr($file, '.', TRUE);
    echo '<li><a class="" href="folder/'.$file.'">'.$name.'</a></li>';

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download