PHP glob() doesnt find .htaccess


PHP glob() doesnt find .htaccess

简单的问题 - 如何使用glob()列出.htaccess文件?

glob()确实列出了"隐藏"文件(以.开头的文件,包括目录...(,但前提是您明确要求它:

 glob(".*");

使用 preg_grep 过滤返回的 glob() 数组以查找.htaccess条目:

 $files = glob(".*") AND $files = preg_grep('/'.htaccess$/', $files);

当然,glob 的替代方案只是使用 scandir() 和过滤器(fnmatch 或正则表达式(:

 preg_grep('/^'.'w+/', scandir("."))

如果有人来到这里,

由于SPL用PHP实现的,并提供了一些很酷的迭代器,因此您可以使用 列出您的隐藏文件,例如.htaccess文件或其替代隐藏的Linux文件。

使用 DirectoryIterator 列出所有目录内容,并排除...,如下所示:

$path = 'path/to/dir';
$files = new DirectoryIterator($path);
foreach ($files as $file) {
    // excluding the . and ..
    if ($file->isDot() === false) {
        // make some stuff
    }
}