Avatar of Cybervanes
Cybervanes asked on

Compiling a php multidimensional array with the contents of a server directory (including subdirectories)

The below code complies an array with the contents of a root directory and the contents of all sub directories.

example:
Array ( [Contracts] => Array ( [0] => abc .txt [New folder] => Array ( ) [Signed Contract] => Array ( ) [Testing subfolder] => Array ( [testing subfolder 2 deep] => Array ( [3 deep] => Array ( ) ) ) ) [Energy Company Documents] => Array ( [0] => leadville bills.pdf [1] => leadville liquors energy usage.pdf [2] => leadville.pdf ) [Engineering] => Array ( [0] => Leadville Liquors PV Watts.docx [1] => Leadville Liquors String Sizing.docx [Renderings] => Array ( [BestExportedImage] => Array ( [0] => main bldg.jpg [1] => Thumbs.db ) [Exported Images] => Array ( ) [0] => main bldg.skp [1] => title block.vsd [2] => Title Block1.layout ) [2] => Thumbs.db [3] => Title Block.pdf ) [Grants-Special Financing] => Array ( ) [Project Management] => Array ( [0] => Estimate - Template.xlsx ) )

I need some ideas on how to rework the code below to arrange the array differently.

I need to display folders first, files second in each level of the array. Instead of everything in alphabetical order


example of how i need the array to be structured:
Array ( [Contracts] => Array ( [New folder] => Array ( ) [Signed Contract] => Array ( ) [Testing subfolder] => Array ( [testing subfolder 2 deep] => Array ( [3 deep] => Array ( ) ) ) [0] => abc .txt ) [Energy Company Documents] => Array ( [0] => leadville bills.pdf [1] => leadville liquors energy usage.pdf [2] => leadville.pdf ) [Engineering] => Array ( [Renderings] => Array ( [BestExportedImage] => Array ( [0] => main bldg.jpg [1] => Thumbs.db ) [Exported Images] => Array ( ) [0] => main bldg.skp [1] => title block.vsd [2] => Title Block1.layout )[0] => Leadville Liquors PV Watts.docx [1] => Leadville Liquors String Sizing.docx [2] => Thumbs.db [3] => Title Block.pdf ) [Grants-Special Financing] => Array ( ) [Project Management] => Array ( [0] => Estimate - Template.xlsx ) )




function list_files($path)
{
    global $filesToIgnore;
	$files = array();
    if(is_dir($path))
    {
        if($handle = opendir($path))
        {
            while(($name = readdir($handle)) !== false)
            {
				if(in_array($name,$filesToIgnore)){ continue; }
                if(!preg_match("#^\.#", $name))
                if(!is_dir($path . "/" . $name))
                {
					$files[] = $name;
                }
                else
                {
                   $files[$name] = list_files($path . "/" . $name); 
                }
            }
            closedir($handle);
        }
    }
    return $files;
}

Open in new window


Any Ideas?
PHPHTMLAJAX

Avatar of undefined
Last Comment
Cybervanes

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Cybervanes

Thanks! Worked Perfectly!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy