仅使用PHP隐藏文件

仅使用PHP隐藏文件,php,Php,我必须从php中隐藏一些扩展名为:.png、.php和.html的文件 .htaccess工作正常 IndexIgnore *.png IndexIgnore *.php IndexIgnore *.html 但是,我想用PHP隐藏这些文件 我使用以下脚本: <?php $myfolder = realpath(dirname(__FILE__)); $handle = opendir("$myfolder"); while($name = readdir($ha

我必须从php中隐藏一些扩展名为:.png、.php和.html的文件

.htaccess工作正常

IndexIgnore *.png
IndexIgnore *.php
IndexIgnore *.html
但是,我想用PHP隐藏这些文件

我使用以下脚本:

<?php
    $myfolder = realpath(dirname(__FILE__));
    $handle = opendir("$myfolder");
    while($name = readdir($handle)) 
        echo "$name<br>";
    }
    closedir($handle);
?>

但是,我仍然可以看到这些文件。
感谢所有能够提供帮助的人。

PHP直接读取文件系统,而不是通过Web服务器读取文件。因此,它会忽略.htaccess文件


您需要手动检查循环中的这些文件类型,然后忽略它们。

PHP直接读取文件系统,而不是通过web服务器读取文件。因此,它会忽略.htaccess文件


您需要手动检查循环中的这些文件类型,然后忽略它们。

下面介绍如何使用regex进行此操作

<?php
    // Regex with which to hide some file types
    $ignore_regex = '/(\.png|\.php|\.html)$/';

    $myfolder = realpath(dirname(__FILE__));
    $handle = opendir("$myfolder");
    while($name = readdir($handle)) {
        // Check if this name matches the ignore regex
        if(preg_match($ignore_regex, $name)) {
            continue;
        }
        echo "$name<br>";
    }
    closedir($handle);
?>

以下是如何使用正则表达式执行此操作

<?php
    // Regex with which to hide some file types
    $ignore_regex = '/(\.png|\.php|\.html)$/';

    $myfolder = realpath(dirname(__FILE__));
    $handle = opendir("$myfolder");
    while($name = readdir($handle)) {
        // Check if this name matches the ignore regex
        if(preg_match($ignore_regex, $name)) {
            continue;
        }
        echo "$name<br>";
    }
    closedir($handle);
?>

PHP调用了一个函数,该函数将返回与模式匹配的文件。这可能就是你需要使用的

下面有一个非常有用的简短教程

这个名称值得单独使用:)

PHP调用了一个函数,该函数将返回与模式匹配的文件。这可能就是你需要使用的

下面有一个非常有用的简短教程

仅就名称而言,它就值得使用:)