0

I have the code, where where everything works, but I need some change. I have PHP code, which show folder tree in select input. But now I can only choose the files in the folder but I only need to choose folders and do not show up files in subfolders. After this I need to show the destination of the choose folder under select input.

I found solution which I need to change.

function dirToOptions($path = "../files/projects", $level = 0) {
        $items = scandir($path);
        foreach($items as $item) {
            // ignore items strating with a dot (= hidden or nav)
            if (strpos($item, '.') === 0) {
                continue;
            }

            $fullPath = $path . DIRECTORY_SEPARATOR . $item;
            // add some whitespace to better mimic the file structure
            $item = str_repeat(' ', $level * 3) . $item;
            // file
            if (is_file($fullPath)) {
                echo "<option>$item</option>";
            }
            // dir
            else if (is_dir($fullPath)) {
                // immediatly close the optgroup to prevent (invalid) nested optgroups
                echo "<optgroup label='$item'></optgroup>";
                // recursive call to self to add the subitems
                dirToOptions($fullPath, $level + 1);
            }
        }

    }

    echo '<select>';
    dirToOptions();
    echo '</select>';
Cyrus
  • 84,225
  • 14
  • 89
  • 153
pulitr
  • 59
  • 1
  • 11
  • Possible duplicate of [PHP Get all subdirectories of a given directory](https://stackoverflow.com/questions/2524151/php-get-all-subdirectories-of-a-given-directory) – Nick Jan 27 '19 at 23:00

1 Answers1

0

I'm not sure if i'll answer your question correctly. You only wanna show up directories but ignore files, right?

So what you can do is using glob - example.

Sebastian Waldbauer
  • 674
  • 1
  • 10
  • 17
  • Rather than posting an answer which consists of nothing other than a link to another question, flag this question as a duplicate of that one. – Nick Jan 27 '19 at 23:00