PHP glob()找不到.htaccess
问题描述:
Simple question - How to list .htaccess
files using glob()
?
简单问题 - 如何使用 glob()列出
.htaccess code>文件? p>
div>
答
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("."))
答
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
}
}