views:

80

answers:

1

Hi,

I'm using the following code to get an array of directories and their sub-directories where each contain file type extension: png. It works great but I need to be able to output the results of the array in a list style format e.g.

* Test
  -> test2.png
  -> test1.png
  * subfolder
    -> test3.png
    * sub sub folder
      -> test4.png

etc

Code:

$filter=".png";  
$directory='../test';  
$it=new RecursiveDirectoryIterator("$directory");
foreach(new RecursiveIteratorIterator($it) as $file){  
    if(!((strpos(strtolower($file),$filter))===false)||empty($filter)){  
        $items[]=preg_replace("#\\\#", "/", $file);  
    }
}

Example array of results:

array (
  0 => '../test/test2.png',
  1 => '../test/subfolder/subsubfolder/test3.png',
  2 => '../test/subfolder/test3.png',
  3 => '../test/test1.png',
)

What would be the best way of achieving the desired result?

A: 

In your if-clause, try:

$items[]=preg_replace("#\\\#", "/", $file->getPathName());

This should give you an output close to the one you desire. However, getPathName outputs absolute paths.

Turbotoast
Thanks.But I'm after displaying it as formatted text on a website e.g.Directory-> - Image - Image - Directory -> - Image - ImageSo one can see a structural representation of the data.
Ah, okay. Misunderstood you there.
Turbotoast