I found the following function, which displays a list of file that match a pattern in a directory tree:
Code: Select all
function find($dir, $pattern){
$dir = escapeshellcmd($dir);
$files = glob("$dir/$pattern");
foreach (glob("$dir/{.[^.]*,*}", GLOB_BRACE|GLOB_ONLYDIR) as $sub_dir) //look in subdirectories
{
$arr = find($sub_dir, $pattern); //recursive call
$files = array_merge($files, $arr); // merge array with files from subdirectory
}
return $files;
}
$txtFiles=find("project/sys","*.txt");
foreach ($txtFiles as $txtFile)
{
echo '<h2>'.$txtFile.'</h2>'; //display all text files
}
I'll give an example of the output I'm looking for:
Folder list
project/sys/diagram
project/sys/bin
project/sys/scripts
project/sys/styles
project/sys/meta
The above folders may not contain a text file directly, but their children folders do. However, I'm not interested in their children folder and I want to list the above level folder only once for each folder.
Thanks in advance