tags:

views:

118

answers:

2

I'm using the jquery plugin jqueryFileTree with the PHP connector (slightly modified to give directories individual classes) to show the contents of a directory. I want to, however, hide all file extensions. Has anyone done something similar? I can think of one or two ways of implementing it but they seem overly-complicated...

Is there a relatively simple way of doing this in PHP?

+4  A: 

Looking at the PHP connector code, you want to replace this:

// All files
foreach( $files as $file ) {
    if( file_exists($root . $_POST['dir'] . $file) && $file != '.' && $file != '..' && !is_dir($root . $_POST['dir'] . $file) ) {
        $ext = preg_replace('/^.*\./', '', $file);
        echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>";
    }
}

With this:

// All files
foreach( $files as $file ) {
    if( file_exists($root . $_POST['dir'] . $file) && $file != '.' && $file != '..' && !is_dir($root . $_POST['dir'] . $file) ) {
        $parts = explode(".", $file);
        $ext = array_pop($parts);
        $name = implode(".", $parts);
        echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($name) . "</a></li>";
    }
}

Please note that the code in this provided connector script is not all that safe and you should take steps to prevent users abusing it to get access to sensitive folders.

Paolo Bergantino
A: 

Look at directoryIterator class and pathinfo() function.

raspi