【问题标题】:How can I hide file extensions in PHP?如何在 PHP 中隐藏文件扩展名?
【发布时间】:2009-04-02 17:52:29
【问题描述】:

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

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

【问题讨论】:

    标签: php jquery


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      查看directoryIterator 类和pathinfo() 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        相关资源
        最近更新 更多