<?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();
?>
$li->appendChild($e);
"Uncaught Error: Call to a member function appendChild() on null"