We help IT Professionals succeed at work.
Get Started

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

Cybervanes
Cybervanes asked
on
355 Views
Last Modified: 2012-05-10
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?
Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE