PHP脚本遍历目录/文件树和输出树作为嵌套的UL [英] PHP script to traverse directory/file tree and output tree as nested ULs

查看:144
本文介绍了PHP脚本遍历目录/文件树和输出树作为嵌套的UL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一棵目录,子目录和文件树(在某些但不是全部的目录)。以下是整棵树的一个例​​子:

  / food 
/ food / drinks
/ 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
/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/colors/red.html
/化妆品/lipstick/colors/pink.html
/cosmetics/lipstick/colors/purple.html

OK,从'/'目录中的php脚本,我想递归或遍历这个目录树,并打印出这样的树:

 < 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>化妆品< / li>
< ul>
< li>香水< / li>
< ul>
< li> chic.html< / li>
< li> polo.html< / li>
< li> lust.html< / li>
< / ul>
< li>口红< / li>
< ul>
< li> colors< / li>
< ul>
< li> red.html< / li>
< li> pink.html< / li>
< li> purple.html< / li>
< / ul>
< / ul>
< / ul>
< / ul>


解决方案

我认为你在这里需要的是 RecursiveDirectoryIterator PHP标准库(SPL)



然后可以写下类似于以下内容的内容:

  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);


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆