用于遍历目录/文件树和输出树的PHP脚本作为嵌套UL

时间:2012-05-25 04:14:29

标签: php html arrays recursion directory

我有一个目录,子目录和文件树(在一些但不是所有目录中)。这是整棵树的一个例​​子:

/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

好的,从'/'目录中的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>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>

4 个答案:

答案 0 :(得分:8)

我认为您需要的是RecursiveDirectoryIterator

中的PHP Standard Library (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);

答案 1 :(得分:6)

要开始使用,需要使用以下功能

要将它们粘合在一起,您需要

以上是在粘贴上述所有资源之后的情况

<?php

function read_dir_content($parent_dir, $depth = 0){
    $str_result = "";

    $str_result .= "<li>". dirname($parent_dir) ."</li>";
    $str_result .= "<ul>";
    if ($handle = opendir($parent_dir)) 
    {
        while (false !== ($file = readdir($handle)))
        {
            if(in_array($file, array('.', '..'))) continue;
            if( is_dir($parent_dir . "/" . $file) ){
                $str_result .= "<li>" . read_dir_content($parent_dir . "/" . $file, $depth++) . "</li>";
            }
            $str_result .= "<li>{$file}</li>";
        }
        closedir($handle);
    }
    $str_result .= "</ul>";


    return $str_result;
}


echo "<ul>".read_dir_content("d:/movies/")."</ul>";

答案 2 :(得分:2)

我用它来拥有目录中的所有文件:

function tree($path, $exclude = array()) {
    $files = array();
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $splfileinfo) {
        if (!in_array(basename($splfileinfo), $exclude)) {
            $files[] = str_replace($path, '', '' . $splfileinfo);
        }
    }
    return $files;
}

答案 3 :(得分:1)

希望此功能可以帮助您完成工作。

/*
     * Retern the file list which include in the given directory
     *
     * @param String $dirpath : Path to the directory
     * 
     * @access public
     * 
     * @return Array
     */
    function getFileList($dirpath)
    {
        $filelist = array();
        if ($handle = opendir(dirname ($dirpath))) 
        {
           while (false !== ($file = readdir($handle)))
              {
                    $filelist[] = $file;
              }
            closedir($handle);
        }

        return $filelist;
    }