PHP: How to populate a directory structure in an array

前端 未结 4 963
Happy的楠姐
Happy的楠姐 2021-01-06 05:43

I am developing an admin panel that shows the directory structure of a specific location on server. I have got a recursive php function that iterates through every file and

4条回答
  •  误落风尘
    2021-01-06 06:08

    You could try this, where $dir is a specified path:

    function dir_tree($dir) {
       $path = '';
       $stack[] = $dir;
       while ($stack) {
           $thisdir = array_pop($stack);
           if ($dircont = scandir($thisdir)) {
               $i=0;
               while (isset($dircont[$i])) {
                   if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
                       $current_file = "{$thisdir}/{$dircont[$i]}";
                       if (is_file($current_file)) {
                           $path[] = "{$thisdir}/{$dircont[$i]}";
                       } elseif (is_dir($current_file)) {
                            $path[] = "{$thisdir}/{$dircont[$i]}";
                           $stack[] = $current_file;
                       }
                   }
                   $i++;
               }
           }
       }
       return $path;
    }
    

提交回复
热议问题