Write a function to traverse the files and subfolders in the folder according to the foreground output format.

Source: Internet
Author: User
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);

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.