We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19889
    • 616 Posts
    Hello,

    I need a little help with PHP.

    Does anybody know how I would use the script below?

    Thank you

    # 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); 
    
      • 18397
      • 3,250 Posts
      MARKSVIRTUALDESK Reply #2, 21 years ago
      Looks like it makes an unordered list out of an array but I could be wrong.
        • 19889
        • 616 Posts
        Thanks for getting back to me.

        I can see that but what I was wondering how I would pass the array to that script.

        What I’m basically looking for is a script that would show my directory structure in a clean unordered list.

        If I pass the following to the list script, it doesn’t work.

        <?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 ) ;
        }
        
        ?> 
        
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Maybe
          $path = "assets/images/";
          setDirTree(searchdir($path));
          

            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 34162
            • 1 Posts
            I add a few lines at the bottom and it works.

            <?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 ) ;
            }

            // Make a nice list of the current directory
            echo "<ul>";
            foreach (searchdir(".") as $v) {
            echo "<li>".$v."</li>\n";
            }
            echo "</ul>";

            ?>
              • 19889
              • 616 Posts
              this seems to work now:

              <?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')); 
              
              
              ?>