Home > Enterprise >  Recursive folder tree in PHP
Recursive folder tree in PHP

Time:10-30

I want to make a folder tree array in php. I coded something, it is almost done but there is some issue.

So, all files and folders should be in same json as they are in folders. Here my codes:


function getDirContents($dir, &$results = array(), $counter = 0 ) {
    $files = scandir($dir);

    foreach ($files as $key => $value) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
        if (!is_dir($path)) {
            $results[] = array('name'=>$path,'type'=>'file');
            
        } else if ($value != "." && $value != "..") {
            $results[] = array('name'=>$path,'type'=>'folder','subfolders'=>array());
            
            getDirContents($path, $results[count($results)]['subfolders']);
            
        }
    }

    return $results;
}


print_r(json_encode(getDirContents('./')));

The result is like below. But it has subfolders label at different place. For example; subfolder test4 should be in test2 subfolder buut it is district from test2 folder.

[
   {
      "name":"C:\\xampp\\htdocs\\test\\test.php",
      "type":"file"
   },
   {
      "name":"C:\\xampp\\htdocs\\test\\test.txt",
      "type":"file"
   },
   {
      "name":"C:\\xampp\\htdocs\\test\\test1",
      "type":"folder",
      "subfolders":[
         
      ]
   },
   {
      "subfolders":[
         {
            "name":"C:\\xampp\\htdocs\\test\\test1\\test2",
            "type":"folder",
            "subfolders":[
               
            ]
         },
         {
            "subfolders":[
               {
                  "name":"C:\\xampp\\htdocs\\test\\test1\\test2\\test4",
                  "type":"folder",
                  "subfolders":[
                     
                  ]
               },
               {
                  "subfolders":null
               }
            ]
         },
         {
            "name":"C:\\xampp\\htdocs\\test\\test1\\test3.txt",
            "type":"file"
         }
      ]
   },
   {
      "name":"C:\\xampp\\htdocs\\test\\test_1.php",
      "type":"file"
   }
]

Result should be like that:

[
  {
    "folder1": {
      "name": "test1.txt",
      "type": "file",
      "subfolders": {
        "subfolder1": {
          "name": "test1",
          "type": "folder"
        },
        "subfolder2": {
          "name": "test2",
          "type": "folder",
          "subfolders": {
            "subfolder3": {
              "name": "test3",
              "type": "folder"
            },
            "subfile":{
              "name":"test3.txt"
              "type":"file"
            }
          }
        }
      }
    }
  }
]

I hope I could explain my situation. At the result, datas is not in one back datas.

CodePudding user response:

The problem is here when you call getDirContents($path, $results[count($results)]['subfolders']); recursivelly, you provide second parameter with wrong index of array. When you store the first folder to $results it has index in array = 0. But then you use count() function to get number of records from $results array it returns 1 not 0. Also I think you have disabled notice error reporting and you just havent seen it. Replace count($results) with array_key_last or count($results) - 1

  • Related