I\'m currently trying to develop a method to get a overview of all my different web templates I\'ve created and (legally) downloaded over the years. I thought about displayi
glob('*.html')
will work if they're all in one directory.
If you want to walk a file tree -- checking everything in the current directory and in subdirectories and subdirectories of subdirectories (etc) -- then you have a couple of options.
One would be to use the unix find
command with one of the methods of PHP system invocation. Something like:
find <search_root_dir> -name "*.html" -print
will get you output that looks something like
search_root_dir/blah.html
search_root_dir/foo.html
search_root_dir/subdir/baz.html
search_root_dir/subdir/bah.html
...
Another thing you could do is write a recursive function that uses chdir
and readdir
or maybe scandir
, something like:
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);
}
}
Then, write another function:
$html_files = array();
function record_html_files($filename) {
global $html_files;
if(strpos($filename,'*.html') === (strlen($filename) - 6))
$html_files[] = $filename;
}
And call it like this:
dir_walk('/path/to/search/root','record_html_files');
Or, write dir_walk so it accepts an object with a method call you can make inside. There's some variations possible here.