Aşağıdaki örneği kullanarak, belirli bir klasör içindeki tüm dosya ve klasörleri listeleyip treeview yaratabilirsiniz. Sonuç her zaman alfabetik olarak sıralanır. Örneğimiz listelenen elementlere icon eklemek için Font Awesome kullanıyor.


Mevcut yapı


cloud
candidates
one
one1.xls
one2.pdf
three
two
a
two
info.txt
info.txt
info.rtf
logo.jpg
readme.txt
clients
one
one
one1.txt
one2.png
one3.doc
one4.docx
two
two1.rtf
readme.txt

Kod


Sayfanıza https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css linkini eklemeyi unutmayın.


class TreeView
{
private $root;

public function __construct($path)
{
$this->root = $path;
}

public function getTree($path = null)
{
$path = $path ? $path : $this->root;
$folders = [];
$files = [];

foreach (scandir($path) as $node) {
if ($this->filter($node)) {
continue;
}

if (is_dir($path.'/'.$node)) {
$folders[$node] = self::getTree($path.'/'.$node);
} else {
$files[$node] = str_replace($this->root.'/', null, $path.'/'.$node);;
}
}

return array_merge($folders, $files);
}

private function filter($filename)
{
return in_array($filename, ['.', '..', 'index.php', '.htaccess']);
}
}

$treeView = new TreeView('../uploads/');
$tree = $treeView->getTree();

function printTree($tree)
{
if ($tree) {
echo '<ul class="fa-ul">';
foreach ($tree as $node => $sub) {
if (strstr($node, '.')) {
echo '<li><i class="fa-li fa fa-file-o" aria-hidden="true"></i>';
echo '<a href="download.php?'.$sub.'">'.$node.'</a>';
echo '</li>';
} else {
echo '<li><i class="fa-li fa fa-folder" aria-hidden="true"></i>'.$node.'</li>';
}

if (is_array($sub)) {
printTree($sub);
}
}
echo '</ul>';
}
}

printTree($tree);

Sonuç