问题
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>
回答1:
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);
回答2:
To get you started, it need the following functions to work:
- opendir() to get the directory handler
- readdir() to read it's content
- is_dir() to determine if current value taken from
readdir()is directory or files
To glue it all together, you will need:
- looper (either for, for each, while)
- recursive algorithm (or infinite while loop)
Here's how it is after all resources above is glued:
<?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>";
回答3:
I use this to have all files in a dir :
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;
}
回答4:
Hope this function will help you to get done your work.
/*
* 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;
}
来源:https://stackoverflow.com/questions/10748387/php-script-to-traverse-directory-file-tree-and-output-tree-as-nested-uls