Lateral view. How does the phpsys-class.admin.php modify code?
function my_scandir($file){ if($f = opendir($file)){ while($r = readdir($f)){ if($r != '..' && $r != '.'){ $c = $file.'/'.$r; if(is_dir($c)){ echo $r.'
'; my_scandir($c); }else{ echo $r.'
'; } } } }}my_scandir('clone2');
Foreground output is
Public
Admin. php
Css
Admin.css
Style.css
View. php
Sys
Class. admin. php
I want this effect
Public
-Admin. php
-Css
--Admin.css
--Style.css
View. php
Sys
-Class. admin. php
How to modify the code?
Reply content:
function my_scandir($file){ if($f = opendir($file)){ while($r = readdir($f)){ if($r != '..' && $r != '.'){ $c = $file.'/'.$r; if(is_dir($c)){ echo $r.'
'; my_scandir($c); }else{ echo $r.'
'; } } } }}my_scandir('clone2');
Foreground output is
Public
Admin. php
Css
Admin.css
Style.css
View. php
Sys
Class. admin. php
I want this effect
Public
-Admin. php
-Css
--Admin.css
--Style.css
View. php
Sys
-Class. admin. php
How to modify the code?
Add a parameter $ depth. The default value is 0.
$ Depth minus signs are output before the file name.
Then, $ depth is added to 1 for each recursion and then called.
Common Syntax:
function read_dir_content($parent_dir, $depth = 0){ $str_result = ""; $str_result .= "". dirname($parent_dir) .""; $str_result .= "
"; if ($handle = opendir($parent_dir)) { while (false !== ($file = readdir($handle))) { if(in_array($file, array('.', '..'))) continue; if( is_dir($parent_dir . "/" . $file) ){ $str_result .= "
- " . read_dir_content($parent_dir . "/" . $file, $depth++) . "
"; } $str_result .= "
- {$file}
"; } closedir($handle); } $str_result .= "
"; return $str_result;}echo "
" . read_dir_content("/folder") . "
";
If your php is> 5.31:
function iterateDirectory($i){ echo '
'; foreach ($i as $path) { if ($path->isDir()) { echo '
- '; iterateDirectory($path); echo '
'; } else { echo '
- '.$path.'
'; } } echo '
';}$dir = '/folder';$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));iterateDirectory($iterator);