Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Directory Structure (Strings) to JSON using PHP

I have an array of file-path strings like this

  • videos/funny/jelloman.wmv
  • videos/funny/bellydance.flv
  • videos/abc.mp4
  • videos/june.mp4
  • videos/cleaver.mp4
  • audio/uptown.mp3
  • audio/juicy.mp3
  • fun.wmv
  • jimmy.wmv
  • herman.wmv

End goal is to get them to jsTree. I built a prototype tree from the above sample strings. check it out: http://jsfiddle.net/ecropolis/pAqas/

like image 459
Ecropolis Avatar asked Feb 13 '11 23:02

Ecropolis


2 Answers

I was able to use this excellent solution (the bottom one posted by @Casablanca) to process the above strings into a recursive array structure. Convert array of paths into UL list

<?php
    $paths = array('videos/funny/jelloman.wmv','videos/funny/bellydance.flv','videos/abc.mp4','videos/june.mp4','videos/cleaver.mp4','audio/uptown.mp3','audio/juicy.mp3','fun.wmv', 'jimmy.wmv','herman.wmv');
    sort($paths); 
    $array = array();
    foreach ($paths as $path) {
      $path = trim($path, '/');
      $list = explode('/', $path);
      $n = count($list);

      $arrayRef = &$array; // start from the root
      for ($i = 0; $i < $n; $i++) {
        $key = $list[$i];
        $arrayRef = &$arrayRef[$key]; // index into the next level
      }
    }

    function buildUL($array, $prefix,$firstrun) {     
        $c = count($array);
      foreach ($array as $key => $value) {
            $path_parts = pathinfo($key);
            if($path_parts['extension'] != '') {
                $extension = $path_parts['extension'];
            } else {
                $extension = 'folder';
            }
            if ($prefix == '') { //its a folder
                echo ' { "data":"'.$key.'"';
            } else { //its a file
                echo '{"data" : {"title":"'.$key.'"},"attr":{"href": "'.$prefix.$key.'","id": "1239"},
                "icon": "images\/'.$extension.'-icon.gif"';
            }
            // if the value is another array, recursively build the list$key
            if (is_array($value)) {
                echo ',"children" : [ ';
                buildUL($value, "$prefix$key/",false);
            }
            echo "}";
            $c = $c-1;
            if($c != 0) {
                echo ",";
            }
      } //end foreach
     if($firstrun != true) 
      echo "]";
    }

    echo '{ "data" : [';
    buildUL($array, '',true);
    echo '] }';
?> 
like image 79
Ecropolis Avatar answered Sep 27 '22 21:09

Ecropolis


Firstly I would create a recursive function to iterate through your directory into an array

function ReadFolderDirectory($dir,$listDir= array())
{
    $listDir = array();
    if($handler = opendir($dir))
    {
        while (($sub = readdir($handler)) !== FALSE)
        {
            if ($sub != "." && $sub != ".." && $sub != "Thumb.db")
            {
                if(is_file($dir."/".$sub))
                {
                    $listDir[] = $sub;
                }elseif(is_dir($dir."/".$sub))
                {
                    $listDir[$sub] = $this->ReadFolderDirectory($dir."/".$sub); 
                } 
            } 
        }    
        closedir($handler); 
    } 
    return $listDir;    
}

and then output the array with json_encode.

Source used from: http://www.php.net/manual/en/function.readdir.php#87733

like image 31
RobertPitt Avatar answered Sep 27 '22 21:09

RobertPitt