You can try this function:
function getDirectory($path = '.', $level = 1)
{
$result = array();
$ignore = array('nbproject', 'src', '.', '..');
$dh = @opendir($path);
$i = 0;
while($file = readdir($dh))
{
if(!in_array($file, $ignore))
{
if(is_dir($path.'/'.$file))
{
$level++;
$singleResult = array('title'=>$file, 'isFolder'=>true, 'children'=>$this->getDirectory($path.'/'.$file, $level), 'key'=>'node'.$level);
$result[] = $singleResult;
}
else
{
$result[] = array('title'=>$file, 'key'=>'node'.$level.'.'.$i);
}
}
$i++;
}
closedir($dh);
return $result;
}
I scanned with this function one of my directories and it outputs:
array
(
0 => array
(
'title' => 'CHANGELOG.txt'
'key' => 'node1.2'
)
1 => array
(
'title' => 'composer.json'
'key' => 'node1.3'
)
2 => array
(
'title' => 'docs'
'isFolder' => true
'children' => array
(
0 => array
(
'title' => 'COMPUTER-FUNCTIONS.md'
'key' => 'node2.2'
)
1 => array
(
'title' => 'CONFIGURATION.md'
'key' => 'node2.3'
)
2 => array
(
'title' => 'CONTACT-FUNCTIONS.md'
'key' => 'node2.4'
)
3 => array
(
'title' => 'EXCHANGE-FUNCTIONS.md'
'key' => 'node2.5'
)
4 => array
(
'title' => 'FOLDER-FUNCTIONS.md'
'key' => 'node2.6'
)
5 => array
(
'title' => 'GETTING-STARTED.md'
'key' => 'node2.7'
)
6 => array
(
'title' => 'GROUP-FUNCTIONS.md'
'key' => 'node2.8'
)
7 => array
(
'title' => 'SEARCH-FUNCTIONS.md'
'key' => 'node2.9'
)
8 => array
(
'title' => 'UPGRADING.md'
'key' => 'node2.10'
)
9 => array
(
'title' => 'USER-FUNCTIONS.md'
'key' => 'node2.11'
)
)
'key' => 'node2'
)
3 => array
(
'title' => 'examples'
'isFolder' => true
'children' => array
(
0 => array
(
'title' => 'examples.php'
'key' => 'node3.2'
)
1 => array
(
'title' => 'groupCollection.php'
'key' => 'node3.3'
)
2 => array
(
'title' => 'index.php'
'key' => 'node3.4'
)
3 => array
(
'title' => 'userCollection.php'
'key' => 'node3.5'
)
4 => array
(
'title' => 'view.html.php'
'key' => 'node3.6'
)
)
'key' => 'node3'
)
4 => array
(
'title' => 'LICENSE.txt'
'key' => 'node3.6'
)
5 => array
(
'title' => 'phpunit.xml'
'key' => 'node3.8'
)
6 => array
(
'title' => 'README.md'
'key' => 'node3.9'
)
7 => array
(
'title' => 'tests'
'isFolder' => true
'children' => array
(
0 => array
(
'title' => 'AdldapBaseTest.php'
'key' => 'node4.2'
)
1 => array
(
'title' => 'AdldapConstructTest.php'
'key' => 'node4.3'
)
2 => array
(
'title' => 'AdldapLiveTest.php'
'key' => 'node4.4'
)
3 => array
(
'title' => 'AdldapMethodTest.php'
'key' => 'node4.5'
)
4 => array
(
'title' => 'AdldapObjectTest.php'
'key' => 'node4.6'
)
5 => array
(
'title' => 'Classes'
'isFolder' => true
'children' => array
(
0 => array
(
'title' => 'AdldapSearchTest.php'
'key' => 'node5.2'
)
1 => array
(
'title' => 'AdldapUsersTest.php'
'key' => 'node5.3'
)
2 => array
(
'title' => 'AdldapUtilityTest.php'
'key' => 'node5.4'
)
)
'key' => 'node5'
)
6 => array
(
'title' => 'ConnectionTest.php'
'key' => 'node5.8'
)
7 => array
(
'title' => 'FunctionalTestCase.php'
'key' => 'node5.9'
)
8 => array
(
'title' => 'Objects'
'isFolder' => true
'children' => array
(
0 => array
(
'title' => 'AccountControlTest.php'
'key' => 'node6.2'
)
1 => array
(
'title' => 'ContactTest.php'
'key' => 'node6.3'
)
2 => array
(
'title' => 'LdapEntryTest.php'
'key' => 'node6.4'
)
3 => array
(
'title' => 'MailboxTest.php'
'key' => 'node6.5'
)
4 => array
(
'title' => 'PagintorTest.php'
'key' => 'node6.6'
)
5 => array
(
'title' => 'UserTest.php'
'key' => 'node6.7'
)
)
'key' => 'node6'
)
)
'key' => 'node4'
)
)
Of course you need to implement converting array to json by yourself, there is just method for scanning directories and giving them proper parameters.