# UL Tree----------------------------------------------------#
function SetDirTree($MultiArray,$depth=-1,$stack=0)
{
foreach($MultiArray as $k=>$v)
{
if (is_array($v))
{
$depth++;
if ($olddepth!=$depth)
{
$stack++;
echo "\n".'<ul>';
}
if ($element>0) echo '</li>';
echo "\n".'<li>'.$k.'';
$element++;
$olddepth = $depth;
SetDirTree($v,$depth,$stack);
$depth--;
}
}
if ($depth!=$stack) echo '</li></ul>';
}
echo '<ul>';
SetDirTree($testlist);
<?php
// $path : path to browse
// $maxdepth : how deep to browse (-1=unlimited)
// $mode : "FULL"|"DIRS"|"FILES"
// $d : must not be defined
function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
{
if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }
$dirlist = array () ;
if ( $mode != "FILES" ) { $dirlist[] = $path ; }
if ( $handle = opendir ( $path ) )
{
while ( false !== ( $file = readdir ( $handle ) ) )
{
if ( $file != '.' && $file != '..' )
{
$file = $path . $file ;
if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) { $dirlist[] = $file ; } }
elseif ( $d >=0 && $d < $maxdepth )
{
$result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
$dirlist = array_merge ( $dirlist , $result ) ;
}
}
}
closedir ( $handle ) ;
}
if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
return ( $dirlist ) ;
}
?>
<?php
//-----------------------------------------------------------*/
function dirTreeToArray( $path = "./", $style="0" ) {
if ( !is_string($path) or !($style==0 or $style==1) ) die("Improper arguments in function dirTreeToArray(string \$path, \$style = {0, 1})");
else {
$path = trim($path);
$path = str_replace("\\", "/", $path);
if( '/' == $path{strlen($path)-1} || '\'' == $path{strlen($path)-1} ) $path = substr($path, 0, strlen($path)-1);
}
$tree = array();
$arrayPointer = & $tree;
if ( $handle = opendir($path) ) {
while ( $fileName = readdir($handle) ) {
if ( $fileName != "." and $fileName != ".." ) {
$newPath = "$path/$fileName"; // Used for testing directory or file property
if ( is_dir($newPath) ) {
$dummyPointer = & $arrayPointer; // Save the current pointer
if ( $style == 0 ) $arrayPointer = & $arrayPointer[$fileName]; // Drop the pointer into a subdirectory
elseif ( $style == 1 ) $arrayPointer = & $arrayPointer[]; // Drop the pointer into a subdirectory
$arrayPointer = dirTreeToArray($newPath, $style); // Process the new subdirectory
if ( $style == 1 ) sort($arrayPointer); // Sort it if we're in a non-associative mode
$arrayPointer = & $dummyPointer; // Bring the pointer back up a level
}
//elseif ( is_file($newPath) ) $arrayPointer[] = $fileName; // Enter the files
}
}
return $tree;
}
else die("Failed to open directory $path in function dirTreeToArray(string \$path, \$style = {0, 1})");
}
# UL Tree----------------------------------------------------#
function SetDirTree($MultiArray,$depth=-1,$stack=0)
{
foreach($MultiArray as $k=>$v)
{
if (is_array($v))
{
$depth++;
if ($olddepth!=$depth)
{
$stack++;
echo "\n".'<ul>';
}
if ($element>0) echo '</li>';
echo "\n".'<li>'.$k.'';
$element++;
$olddepth = $depth;
SetDirTree($v,$depth,$stack);
$depth--;
}
}
if ($depth!=$stack) echo '</li></ul>';
}
echo '<ul>';
SetDirTree(dirTreeToArray('D:/www/dev/im/assets/images'));
?>