0

So I've got this piece of code that works perfectly for displaying all the subdirectories of a predefined directory (''myfolder/files'') as radio buttons in a form. However, it doesn't display any subfolders within the folders it detects. How do I get it to display the directory's subfolders, and the folders within that, etc?

<?php 
$filespath = "../myfolder/files/";
$dir = new DirectoryIterator($filespath);
    foreach ($dir as $fileinfo) {
        if ($fileinfo->isDir() && !$fileinfo->isDot()) {
            echo '<input type="radio" name="folder" value="../myfolder/files/'.$fileinfo->getFilename().'/"/> '.$fileinfo->getFilename().' <br>';
        }
    }
?>

For example, ../myfolder/files/ looks like this:

../myfolder/files/images

../myfolder/files/images/photos

../myfolder/files/images/photos/vacation

../myfolder/files/images/photos/family pictures

../myfolder/files/documents

../myfolder/files/documents/stuff

Right now, it only displays ''images'' and ''documents'' but I also want it do display photos, vacation, family pictures, stuff, etc.

Mr X
  • 129
  • 2
  • 16
  • 1
    Research recursion. You'll likely want to create a function that calls itself when it finds a new directory.... (edit) Or you could look into [RecursiveDirectoryIterator](http://php.net/manual/en/class.recursivedirectoryiterator.php) – Devon Bessemer Jun 20 '18 at 14:02
  • 2
    use the RecursiveDirectoryItterator (for lack of a better name) class http://php.net/manual/en/class.recursivedirectoryiterator.php – lovelace Jun 20 '18 at 14:04
  • Possible duplicate of [List all the files and folders in a Directory with PHP recursive function](https://stackoverflow.com/questions/24783862/list-all-the-files-and-folders-in-a-directory-with-php-recursive-function) – hakki Jun 20 '18 at 14:09
  • Before you can [research recursion](https://www.google.com/search?source=hp&ei=92UqW4bQOrSTmwXGsJ_IDg&q=recursion&oq=recursion&gs_l=psy-ab.12...0.0.0.4714.0.0.0.0.0.0.0.0..0.0....0...1c..64.psy-ab..0.0.0....0.XICowEQQ5gA) you must first research recursion. – CD001 Jun 20 '18 at 14:35

0 Answers0