PHP glob() doesnt find .htaccess

江枫思渺然 提交于 2019-12-06 03:02:55

问题


Simple question - How to list .htaccess files using glob()?


回答1:


glob() does list "hidden" files (files starting with . including the directories . and ..), but only if you explicitly ask it for:

 glob(".*");

Filtering the returned glob() array for .htaccess entries with preg_grep:

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

The alternative to glob of course would be just using scandir() and a filter (fnmatch or regex):

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



回答2:


in case any body come to here,

since the SPL implemented in PHP, and offers some cool iterators, you may make use of the to list your hidden files such as .htaccess files or it's alternative hidden linux files.

using DirectoryIterator to list all of directory contents and excluding the . and .. as follows:

$path = 'path/to/dir';
$files = new DirectoryIterator($path);

foreach ($files as $file) {
    // excluding the . and ..
    if ($file->isDot() === false) {
        // make some stuff
    }
}


来源:https://stackoverflow.com/questions/8594413/php-glob-doesnt-find-htaccess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!