5

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

/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

4 回答 4

8

我认为您需要的是PHP标准库 (SPL)中的RecursiveDirectoryIterator

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

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);
于 2012-05-25T07:37:59.770 回答
7

为了让您开始,它需要以下功能才能工作

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

上面的所有资源都粘合后的情况如下:

<?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>";
于 2012-05-25T04:33:25.380 回答
4

我用它来将所有文件放在一个目录中:

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;
}
于 2015-02-16T14:41:15.240 回答
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;
    }
于 2012-05-25T04:25:13.790 回答