glob('*.html')
如果它们都在一个目录中,则可以工作。
如果您想遍历文件树 - 检查当前目录和子目录以及子目录的子目录(等)中的所有内容 - 那么您有几个选择。
一种是使用unixfind
命令与 PHP 系统调用的方法之一。就像是:
find <search_root_dir> -name "*.html" -print
会给你看起来像这样的输出
search_root_dir/blah.html
search_root_dir/foo.html
search_root_dir/subdir/baz.html
search_root_dir/subdir/bah.html
...
您可以做的另一件事是编写一个递归函数,使用chdir
and readdir
或者可能scandir
, 就像是:
function dir_walk($start_dir,$func) {
$entries = scandir($start_dir);
foreach($entries as $entry) {
if($entry == '.' || $entry == '..') {
/*skip these*/
} else if(is_dir($entry)) {
dir_walk($start_dir.'/'.$entry,$func);
} else $func($start_dir.'/'.$entry);
}
}
然后,编写另一个函数:
$html_files = array();
function record_html_files($filename) {
global $html_files;
if(strpos($filename,'*.html') === (strlen($filename) - 6))
$html_files[] = $filename;
}
并这样称呼它:
dir_walk('/path/to/search/root','record_html_files');