I am developing a wordpress meta box and I have to scan a listing of subdirectories that contains images inside my template and add those to a choose dropdown in order to make use of the filename during my template.

The pictures are presently arranged within the folder such as this:

Parent Folder
_ Secondary Folder
   _ Image.png
   _ Image.digital
   _ Image.presen
_ Secondary Folder
   _ Image.png
   _ Image.digital
   _ Image.presen

Ideally Let me keep that structure during my choose dropdown ie.

Secondary Folder .
   _ Image.png

I have used this:

function get_dir_path(){
    return dirname(__FILE__).'/library/images';
}
$largeImagesdir = get_dir_path() . '/960x345/';


    if ($dh = opendir($largeImagesdir)) {
        while (($file = readdir($dh)) !== false) {
            $lfiles .= '<option>' . $file . '</option>';
        }
        closedir($dh);
    }

    $buildbox .= '<select>' . $lfiles . '</select>';

However this obviously only works basically set the $largeImagesdir var to become among the sub sites...

Can anybody help?

You'll need a loop in the loop. Presuming only 2 amounts of sites, inside your while see if $file is really a subdirectory with is_dir(), and when yes perform a readdir() on this too to obtain its options built.

Then, you are able to distinguish the subdirectories by [cde] inside your optgroup element.

If you want multiple different amounts of subdirectories you will need a recursive function to consider proper care of it. The main one proven here is a great beginning point.