Tutorial :How can I hide file extensions in PHP?



Question:

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?


Solution:1

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.


Solution:2

Look at directoryIterator class and pathinfo() function.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »