简体   繁体   English

如何在PHP中隐藏文件扩展名?

[英]How can I hide file extensions in PHP?

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. 我正在使用带有PHP连接器的jquery插件jqueryFileTree (稍作修改以为目录提供单独的类)以显示目录的内容。 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? 在PHP中有相对简单的方法吗?

Looking at the PHP connector code, you want to replace this: 查看PHP连接器代码,您要替换为:

// 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. 请注意,此提供的连接器脚本中的代码并非十分安全,您应采取措施防止用户滥用它来访问敏感文件夹。

查看directoryIterator类和pathinfo()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM