简体   繁体   English

PHP递归目录列表 - 仅限目录

[英]PHP recursive directory listing - directories ONLY

I am working on a file manager project and can't seem to get my head around doing a recursive directory listing where I only get the directories. 我正在研究一个文件管理器项目,似乎无法理解我只获取目录的递归目录列表。 Everything I've tried either leaves off empty sub-directories or won't work when I try to parse it into the return structure to my jQuery tree. 我尝试过的所有内容都会留下空的子目录,或者在我尝试将其解析为jQuery树的返回结构时无效。

I've tried using an array, and the recursive iterators but nothing has work for me so far. 我已经尝试过使用数组和递归迭代器,但到目前为止我没有任何工作。

** Update ** **更新**

Thanks to everyone's input I've whittled my code down to: 感谢大家的意见,我已将我的代码缩减为:

class image_management {

private $_user_id;
private $_user_name;
private $_user_path;    

/**
* Constructor
*/
function __construct($user_id, $user_name) {

  $this->_user_id = $user_id;
  $this->_user_name = $user_name;
  $this->_user_path = ORDER_ROOT.$this->_user_id;
}    

/**
* Cleanup the class and close connections
* 
*/
function __destruct() {
}

/**
* Get Image Folders
* Returns an HTML list of the folders under a users
* directory.
*  
* @param hash $user_id -user id hash
* @param string $user_name - users email address
* @return string - html list
*/
public function getHTMLTree() {

  $html = "<li><a href='#'>";
  $html .= $this->_user_name . "</a><ul>";

  $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_user_path), RecursiveIteratorIterator::SELF_FIRST);
  foreach($objects as $file) {
    if($file->isDir() ) {
      $item = substr($file->getPathname(),strlen($this->_user_path));
      echo $item;
    }
  }
  return $html;
}

} }

This gives me the output: 这给了我输出:

/home/printshi/public_html/test/orders/88a0a01afd3728af8f6710ed874267f3
/Aggie_Stuff
/Misc
/Misc/Christmas
/Misc/Christmas/santa
/Misc/Christmas/frosty
/Misc/test1
/Geek_Stuff

which is getting me closer, my issue now is that what I need to do is wrap all these elements in HTML so that I get an output like: 这让我更接近,我现在的问题是,我需要做的是将所有这些元素包装在HTML中,以便得到如下输出:

<ul>
<li>Aggie_Stuff</li>
<li>Misc<ul>
    <li>Christmas<ul>
        <li>santa</li>
        <li>frosty</li>
        </ul>
    </li>
    <li>test1</li>
    </ul>
<li>Geek_Stuff</li>

The recursion part I understand, it's just getting it to do the HTML that I can't seem to get my head around no matter how hard I try. 我理解的递归部分,它只是让它做HTML,无论我怎么努力,我似乎​​无法理解。

Have a look at the DirectoryIterator class, especially the isDir function. 看看DirectoryIterator类,尤其是isDir函数。

$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
        echo $fileinfo->getFilename() . "\n";
        // recursion goes here.
    }
}

RecursiveIteratorIterator and RecursiveDirectoryIterator can do this for you: RecursiveIteratorIteratorRecursiveDirectoryIterator可以为您执行此操作:

$iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach($iter as $file) {
    if ($file->getFilename() == '.') {
        echo $file->getPath() . PHP_EOL;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM