1

Im trying to produce this for JSTree

[
            {"id":2,"text":"Child node 1"},
            {"id":3,"text":"Child node 2"},
            {
                "text" : "Root node",
                "state" : { "opened" : true },
                "children" : [
                    {
                        "text" : "Child node 1",
                        "state" : { "selected" : true },
                        "icon" : "jstree-file"
                    },
                    { "text" : "Child node 222", "state" : { "disabled" : true } }
                ]
            }
        ]

Im trying to create this via using DirectoryIterator

I got the following off of the php website

   $ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), FilesystemIterator::SKIP_DOTS); 
    $r = array(); 
    foreach ($ritit as $splFileInfo) { 


        $path = $splFileInfo->isDir() 
         ? array($splFileInfo->getFilename() => array()) 
         : array($splFileInfo->getFilename()); 

       for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) { 
       $path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path); 
       } 

       $r = array_merge_recursive($r, $path); 

    } 

This outputs

Array ( [.] => Array ( )

[..] => Array
(
)

[0] => Christmas.docx
[test1] => Array
    (
    )

[test2] => Array
    (
    )
)

I tried modifying it to this

$k = array(); 
foreach ($ritit as $splFileInfo) { 
    //skip if its '.' or '..'
    if ($splFileInfo->getFilename() === '.' || $splFileInfo->getFilename() === '..') {
    continue;
    }

    //if is dir
   if($splFileInfo->isDir()) {

        $path[] = array("text" => $splFileInfo->getFilename(), "children" => array()) ;

        } else {
             $path[] = array("text" => $splFileInfo->getFilename());

    }        

   for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) { 
   $path["children"] = array($ritit->getSubIterator($depth)->current()->getFilename() => $path); 
   } 

   $k = array_merge_recursive($k, $path); 

} 

And this outputs

Array
(
    [test2] => Array
        (
        )

    [0] => Array
        (
            [text] => Christmas.docx
        )

    [1] => Array
        (
            [text] => Christmas.docx
        )

    [2] => Array
        (
            [text] => test1
            [children] => Array
                (
                )

        )

    [3] => Array
        (
            [text] => Christmas.docx
        )

    [4] => Array
        (
            [text] => test1
            [children] => Array
                (
                )

        )

    [5] => Array
        (
            [text] => test2
            [children] => Array
                (
                )

        )

)

Please could you help me with what i'm meant to be doing to iterate through the file structure and return an array that i can convert into the json at the start of the post?


update

credit to : https://stackoverflow.com/a/3556894/1842842

I've got this now :

function requestAccountFolderStructure($dir) {
    $list = array();
    $path = $dir;
    $i = 0;
    foreach (new DirectoryIterator($path) as $file) {
        if ($file->isDot())
            continue;

        if ($file->isDir()) {
            $record = array();
            $record['id'] =  $i;
            $record['text'] =  $file->getFilename();
            $record['children'] = array();            
            $record['path'] = $file->getPathInfo()->getFilename();            
            if (is_dir($dir . '/' . $file)) {
                $record['children'] = requestAccountFolderStructure($dir . '/' . $file);
            }
            $list[] = $record;
            $i++;
        }

    }
    return $list;
}

   $dir = 'folder/';
   $directories = requestAccountFolderStructure($dir);

   foreach($directories as $directory){
       //if(count($directory['children'] === 1)){
           $dir = new DirectoryIterator('folder/' . $directory['text']);
            foreach ($dir as $fileinfo) {
        if (!$fileinfo->isDir()) {
                    $directories[$directory['id']]['children'][] = array("text" => $fileinfo->getFilename(),"icon" => "jstree-file" );
        }
            }
       //}     
   }  

   print_r(json_encode($directories));  

Which produces for me

[{"id":0,"text":"test1","children":[{"text":"1.docx","icon":"jstree-file"}],"path":"folder"},{"id":1,"text":"test2","children":[{"id":0,"text":"New folder","children":[],"path":"test2"},{"text":"2.txt","icon":"jstree-file"}],"path":"folder"}]

i just need to figure out making it iterative in the sub sub folders now

2
  • 1
    "i just need to figure out making it iterative in the sub sub folders now" - Read about recursion, that should do it. Commented Oct 20, 2015 at 14:38
  • Thanks :D got there in the end (added answer), and with code i understand!. I was having trouble (still dont understand it) with this line $path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path); Commented Oct 20, 2015 at 15:01

1 Answer 1

1

credit : https://stackoverflow.com/a/952324/1842842

This code

$fileData = convertDirectoryToArray( new DirectoryIterator( 'folder/' ) );

function convertDirectoryToArray( DirectoryIterator $dir )
{
  $data = array();
  foreach ( $dir as $node )
  {
    if ( $node->isDir() && !$node->isDot() )
    {
      $test = array();
      $test['text'] = $node->getFilename();
      $test['children'] = convertDirectoryToArray( new DirectoryIterator( $node->getPathname() ) );

      $data[] = $test;
    }
    else if ( $node->isFile() )
    {
      $test= array();
      $test['text'] = $node->getFilename();
      $test['icon'] = "jstree-file";
      $data[] = $test;
    }
  }
  return $data;
}

print_r(json_encode($fileData)); 

produces

[{"text":"Christmas.docx"},{"text":"test1","children":[{"text":"1.docx"}]},{"text":"test2","children":[{"text":"2.txt"},{"text":"New folder","children":[{"text":"4.txt"}]}]}]

Which is what i was looking for!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.