<?php
function scan($dir){
$d = array();
$arr = opendir($dir);
while($v = readdir($arr)){
if($v == '.' or $v == '..') continue;
if(!is_dir($dir.DIRECTORY_SEPARATOR.$v)) $d[] = $v;
if(is_dir($dir.DIRECTORY_SEPARATOR.$v)) $d[$v] = scan($dir.DIRECTORY_SEPARATOR.$v);
}
return $d;
}
function list_files($array){
$num = 1;
foreach($array as $k => $v){
if(is_array($v)){ ?>
<li class="li-<?php echo $num;?>">
<a data-toggle="collapse" href="#multiCollapseExample-<?php echo $num; ?>" role="button" aria-expanded="false" aria-controls="multiCollapseExample-<?php echo $num; ?>"><?php echo $k .'--Папка <br>';?></a>
<ul class="ul-list-<?php echo $num; ?> collapse multi-collapse" id="multiCollapseExample-<?php echo $num; ?>">
<li class="sub-li">
<?php list_files($v); ?>
</li>
</ul>
</li>
<?php
$num++;
}
else{
?>
<li class="file"><?php echo $v.'-Файл' . $dir . ' <br>'; ?></li>
<?php }
}
}
list_files( scan('protocols'));
?>
<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/protocols';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$dom = new DomDocument("1.0");
$list = $dom->createElement("ul");
$dom->appendChild($list);
$node = $list;
$depth = 0;
$list->setAttribute('class', "ul-$depth");
foreach($objects as $name => $object){
if ($objects->getDepth() == $depth){
//the depth hasnt changed so just add another li
$li = $dom->createElement('li', $object->getFilename());
$node->appendChild($li);
}
elseif ($objects->getDepth() > $depth){
//the depth increased, the last li is a non-empty folder
$li = $node->lastChild;
$li->setAttribute('class', "folder li-depth-$depth");
$ul = $dom->createElement('ul');
$li->appendChild($ul);
$ul->appendChild($dom->createElement('li', $object->getFilename()));
$ul->setAttribute('class', "folder ul-sub-$depth");
$node = $ul;
}
else{
//the depth decreased, going up $difference directories
$difference = $depth - $objects->getDepth();
for ($i = 0; $i < $difference; $difference--){
$node = $node->parentNode->parentNode;
}
$li = $dom->createElement('li', $object->getFilename());
$li->setAttribute('class', "file-$depth");
$node->appendChild($li);
}
$depth = $objects->getDepth();
}
echo $dom->saveHtml();
?>
if ($objects->getDepth() == $depth){
$li = $dom->createElement('li', $object->getFilename());
$li->setAttribute('class', "file file-$depth");
$node->appendChild($li);
}