1

I want to retrive this type of array from specific path in php for dynatree??? How to retrive file and folder like this array...

    children: [
       {title: "Item 1", key: "node1"},
       {title: "Folder 2", isFolder: true, key: "node2",
           children: [
              {title: "Sub-item 2.1", key: "node2.1"},
              {title: "Sub-item 2.2", key: "node2.2"}
             ]
       },
       {title: "Item 3", key: "node3"}
      ]

I tried this function...

  <?php

      function getDirectory($path = '.', $level = 0) {
           $ignore = array('cgi-bin', '.', '..');
           $dh = @opendir($path);

            while (false !== ( $file = readdir($dh) )) {
              if (!in_array($file, $ignore)) {
                 if (is_dir("$path/$file")) {
                     echo '<li id="key1" class="folder expanded">' . $file;
                     echo '<ul class="folder-content">';
                     getDirectory("$path/$file", ($level + 1));
                     echo '</ul>';
                     echo '</li>';
                  }
              }
          }
      closedir($dh);
     }
  ?>
2
  • Did you tried something? Please show us your code. Commented Mar 14, 2016 at 6:29
  • see i add my tried function... @aslawin Commented Mar 14, 2016 at 6:33

1 Answer 1

1

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.

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

2 Comments

now how to send this value to another php page @aslawin
@KevinPatel first try and then if you will be unable post another question.

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.