0

I am trying to find all html files within directories in a root directory, I have an array with all the directories in and I'm trying to loop through each directory in the array to find the files and create a list but it is not working, any ideas?

$dirs = glob("/Root_directory*", GLOB_ONLYDIR);

foreach($dirs as $dir) {
    $phpfiles = glob($dir . "*.html");

    foreach($phpfiles as $phpfile) {
        echo "<a href=$phpfile>".basename($phpfile)."</a>";
    }
}

I also tried the approach recommended here using recursion and this gave an empty array

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
  • 2
    "it is not working" is not a very clear problem. Can you explain what you expect your code to do and what it does instead? – Stephan Vierkant Feb 16 '17 at 10:41
  • I am expecting it to produce hyperlinks to all html files within each directory within the main directory but it produces nothing instead when the php code is run – Josh Greensmith Feb 16 '17 at 10:43
  • Also, for this kind of problem, you should may look at recursive programming. https://en.wikipedia.org/wiki/Recursion_(computer_science) – Twinfriends Feb 16 '17 at 10:44
  • I had a look at some recursive answers such as [this](http://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu) but that doesn't work to find specific file types – Josh Greensmith Feb 16 '17 at 10:46

1 Answers1

0

This is the recursive method that works after a lot of fiddling.

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),        RecursiveIteratorIterator::SELF_FIRST);
$Regex = new RegexIterator($objects, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH);
foreach($Regex as $name => $Rege){
echo "<a href=$name>".$name."</a> \n";
}