Kez Kez - 2 months ago 21
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

href
of the link.

Attempt:

<?php

$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>';
}

}

closedir($dir);

?>


Any help is much appreciated.

Answer

easier way to do this using PHP's ScanDir

http://php.net/manual/en/function.scandir.php

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

foreach($files as $file){

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

}
Comments