Eli
Eli

Reputation: 4359

RecursiveDirectoryIterator to only scan 1 level

how can I use something ilke RecursiveDirectoryIterator to only show the first level directories not sub directories.

such as

/folder
  |-- folder-1
  |-- folder-2
    |-- sub-folder-1
    | -- subfolder-2

I would like to only show the folder names of folder-1 and folder-2 (and any other folders that may be added in the future dynamically), not any files.

In the end I would like to push each result into an array like this

$clusters = new DirectoryIterator("/agents/");
        $cluster_array = array();

        foreach ($clusters as $fileinfo) {
            if ($fileinfo != "." && $fileinfo != ".." && $fileinfo != "conf") {
                array_push($cluster_array, $fileinfo);
            }
        }
        print_r($cluster_array);

edit: but the array comes out looking like this

Array ( [0] => DirectoryIterator Object ( [pathName:SplFileInfo:private] => [glob:DirectoryIterator:private] => [subPathName:RecursiveDirectoryIterator:private] => ) )

but if i echo out the $fileinfo inside the foreach it show cluster_1 which is what I want, but for some reason its not whats being pushed into the array.

Upvotes: 0

Views: 1650

Answers (1)

Andrej
Andrej

Reputation: 7504

It seems that you should use DirectoryIterator class.

Look at http://www.php.net/manual/en/class.directoryiterator.php

Upvotes: 5

Related Questions