1

I want to fetch folder and sub folder from specific path folder id and sub_folder parent_id. Here my function fetch folder and sub folder but without id and parent id.

I want to array with id and parent id.

    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' => getDirectory($path . '/' . $file, $level), 'key' => 'node' . $level);
                        $result[] = $singleResult;
                     }
                 }

                $i++;
           }

           closedir($dh);

         return $result;
        }

      $dir = "../UserUpload/Documents/source";
      $kevin = getDirectory($dir);

This function give me array like this wiithout id and parent id

   array (size=3)
   0 => 
     array (size=4)
      'title' => string 'mst146' (length=6)
      'isFolder' => boolean true
      'children' => 
         array (size=3)
           0 => 
               array (size=4)
                ...
           1 => 
              array (size=4)
                  ...
           2 => 
              array (size=4)
                  ...
      'key' => string 'node2' (length=5)
   1 => 
      array (size=4)
        'title' => string 't124' (length=4)
        'isFolder' => boolean true
        'children' => 
           array (size=0)
           empty
        'key' => string 'node3' (length=5)
   2 => 
      array (size=4)
        'title' => string 'test' (length=4)
        'isFolder' => boolean true
        'children' => 
           array (size=0)
           empty
        'key' => string 'node4' (length=5)
Kevin Patel
  • 101
  • 1
  • 10

1 Answers1

1

i would suggest you use scandir function inside separate class (for clearer recursion).

  class DirectoryScanner{
      public $scannedData;
      protected $ignored = array('nbproject', 'src', '.', '..');

      public function scanDir($path){

          $filesAndDirs = scandir($path);
          foreach($filesAndDirs as $key => $dirOrFile){
              if(!in_array($dirOrFile, $this->ignored) && is_dir($path . DIRECTORY_SEPARATOR . $dirOrFile)){
                  $this->scannedData[$path][$key] = $dirOrFile;
                  $this->scanDir($path . DIRECTORY_SEPARATOR . $dirOrFile);
              }
          }
      }
  }

it will output two dimensional array, where keys of first dimension is path to where you find directory, second dimension is order of the directory (ignored .&.. and files will cause offset in indexes) and value is name of the directory.

for example:

Array
(
[/var/www/cluster/private/..../2016-03/] => Array
    (
        [21] => chity
        [25] => export-porovnani
        [26] => exporty
    )

[/var/www/cluster/private/..../2016-03/export-porovnani] => Array
    (
        [13] => vsechno
    )

)

you can get data from class by accessing $scannedData property. better would be to create geter and set availability of $scannedData to protected/private

Hope i helped you a bit.

Jimmmy
  • 579
  • 12
  • 26