PHP脚本遍历目录/文件树和输出树嵌套的UL [英] PHP script to traverse directory/file tree and output tree as nested ULs
本文介绍了PHP脚本遍历目录/文件树和输出树嵌套的UL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有目录,子目录和文件(在一些但不是所有的目录)的树。这里是整个树的例子:
/食品
/食品/饮料
/food/drinks/water.html
/food/drinks/milk.html
/food/drinks/soda.html
/食品/主菜
/食品/主菜/热
/food/entrees/hot/hamburger.html
/food/entrees/hot/pizza.html
/食品/主菜/冷
/food/entrees/cold/icecream.html
/food/entrees/cold/salad.html
/化妆品
/化妆品/香水
/cosmetics/perfume/chic.html
/cosmetics/perfume/polo.html
/cosmetics/perfume/lust.html
/化妆品/口红
/化妆品/口红/颜色
/cosmetics/lipstick/colors/red.html
/cosmetics/lipstick/colors/pink.html
/cosmetics/lipstick/colors/purple.html
OK,从/目录中的PHP脚本,我想递归遍历或该目录树,打印树是这样的:
< UL>
<立GT;食品LT; /李>
< UL>
<立GT;饮料< /李>
< UL>
<立GT; water.html< /李>
<立GT; milk.html< /李>
<立GT; soda.html< /李>
< / UL>
<立GT;主菜< /李>
< UL>
<立GT;热< /李>
< UL>
<立GT; hamburger.html< /李>
<立GT; pizza.html< /李>
< / UL>
<立GT;冷LT; /李>
< UL>
<立GT; icecream.html< /李>
<立GT; salad.html< /李>
< / UL>
< / UL>
< / UL>
<立GT;化妆品LT; /李>
< UL>
<立GT;香水< /李>
< UL>
<立GT; chic.html< /李>
<立GT; polo.html< /李>
<立GT; lust.html< /李>
< / UL>
<立GT;口红< /李>
< UL>
<立GT;颜色和LT; /李>
< UL>
<立GT; red.html< /李>
<立GT; pink.html< /李>
<立GT; purple.html< /李>
< / UL>
< / UL>
< / UL>
< / UL>
解决方案
我认为你需要这里是 RecursiveDirectoryIterator 从 PHP标准库(SPL)
然后,您可以编写类似如下的内容:
函数iterateDirectory($ I)
{
呼应'< UL>';
的foreach($ I $作为路径){
如果($ path-> ISDIR())
{
呼应'<立GT;';
iterateDirectory($ PATH);
呼应'< /李>';
}
其他
{
呼应'<立GT;'$路径。'< /李>';
}
}
呼应'< / UL>';
}$ DIR ='/食物;
$ =迭代器新RecursiveIteratorIterator(新RecursiveDirectoryIterator($ DIR));iterateDirectory($迭代器);
I have a tree of directories, sub-directories, and files (in some but not all the directories). Here's an example of the whole tree:
/food
/food/drinks
/food/drinks/water.html
/food/drinks/milk.html
/food/drinks/soda.html
/food/entrees
/food/entrees/hot
/food/entrees/hot/hamburger.html
/food/entrees/hot/pizza.html
/food/entrees/cold
/food/entrees/cold/icecream.html
/food/entrees/cold/salad.html
/cosmetics
/cosmetics/perfume
/cosmetics/perfume/chic.html
/cosmetics/perfume/polo.html
/cosmetics/perfume/lust.html
/cosmetics/lipstick
/cosmetics/lipstick/colors
/cosmetics/lipstick/colors/red.html
/cosmetics/lipstick/colors/pink.html
/cosmetics/lipstick/colors/purple.html
OK, From a php script in the '/' directory, I want to recurse or traverse this directory tree and print the tree like this:
<ul>
<li>food</li>
<ul>
<li>drinks</li>
<ul>
<li>water.html</li>
<li>milk.html</li>
<li>soda.html</li>
</ul>
<li>entrees</li>
<ul>
<li>hot</li>
<ul>
<li>hamburger.html</li>
<li>pizza.html</li>
</ul>
<li>cold</li>
<ul>
<li>icecream.html</li>
<li>salad.html</li>
</ul>
</ul>
</ul>
<li>cosmetics</li>
<ul>
<li>perfume</li>
<ul>
<li>chic.html</li>
<li>polo.html</li>
<li>lust.html</li>
</ul>
<li>lipstick</li>
<ul>
<li>colors</li>
<ul>
<li>red.html</li>
<li>pink.html</li>
<li>purple.html</li>
</ul>
</ul>
</ul>
</ul>
解决方案
What I think you need here is the RecursiveDirectoryIterator from the PHP Standard Library (SPL)
You can then write something similar to the below:
function iterateDirectory($i)
{
echo '<ul>';
foreach ($i as $path) {
if ($path->isDir())
{
echo '<li>';
iterateDirectory($path);
echo '</li>';
}
else
{
echo '<li>'.$path.'</li>';
}
}
echo '</ul>';
}
$dir = '/food';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
iterateDirectory($iterator);
这篇关于PHP脚本遍历目录/文件树和输出树嵌套的UL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文