繁体   English   中英

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

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

我正在使用带有PHP连接器的jquery插件jqueryFileTree (稍作修改以为目录提供单独的类)以显示目录的内容。 但是,我想隐藏所有文件扩展名。 有人做过类似的事情吗? 我可以想到一种或两种方式来实现它,但是它们似乎过于复杂...

在PHP中有相对简单的方法吗?

查看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>";
    }
}

有了这个:

// 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>";
    }
}

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

查看directoryIterator类和pathinfo()函数。

暂无
暂无

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

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