Rumah > Soal Jawab > teks badan
function my_scandir($file){
if($f = opendir($file)){
while($r = readdir($f)){
if($r != '..' && $r != '.'){
$c = $file.'/'.$r;
if(is_dir($c)){
echo $r.'<br>';
my_scandir($c);
}else{
echo $r.'<br>';
}
}
}
}
}
my_scandir('clone2');
前台输出为
public
admin.php
css
admin.css
style.css
view.php
sys
class.admin.php
我想要这样的效果
public
-admin.php
-css
--admin.css
--style.css
view.php
sys
-class.admin.php
如何修改代码?
ringa_lee2017-04-10 16:49:19
普通写法:
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("/folder") . "</ul>";
如果你的php > 5.31:
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 = '/folder';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
iterateDirectory($iterator);