Help me look at this recursive folder function what's wrong?

Source: Internet
Author: User
function foreach_dir($filename,$dir){    if(!is_dir($filename)) return;    $r = opendir($filename);    while($s = readdir($r)){        if($s != '.' && $s != '..'){            echo $s.'
'; // $dir .= $s.'
'; foreach_dir($filename.'/'.$s,$dir); } } return $dir;}echo foreach_dir('templates','');

Output correct
Default
Images
Index.html
Top.jpg
Menu
Document.gif
Documents.gif
Index.html
Sdocument.gif
Sdocuments.gif
Stylesheet.css
Template.htm
Subsilverlike

If you switch to this

function foreach_dir($filename,$dir){    // $dir = '';    // echo $filename;    if(!is_dir($filename)) return;    $r = opendir($filename);    while($s = readdir($r)){        if($s != '.' && $s != '..'){            // echo $s.'
'; $dir .= $s.'
'; foreach_dir($filename.'/'.$s,$dir); } } return $dir;}echo foreach_dir('templates','');

Output
Default
Subsilverlike

I'm putting echo $s. '
Replaced by $dir. = $s. '
The display path is not complete, where is wrong?

Reply content:

function foreach_dir($filename,$dir){    if(!is_dir($filename)) return;    $r = opendir($filename);    while($s = readdir($r)){        if($s != '.' && $s != '..'){            echo $s.'
'; // $dir .= $s.'
'; foreach_dir($filename.'/'.$s,$dir); } } return $dir;}echo foreach_dir('templates','');

Output correct
Default
Images
Index.html
Top.jpg
Menu
Document.gif
Documents.gif
Index.html
Sdocument.gif
Sdocuments.gif
Stylesheet.css
Template.htm
Subsilverlike

If you switch to this

function foreach_dir($filename,$dir){    // $dir = '';    // echo $filename;    if(!is_dir($filename)) return;    $r = opendir($filename);    while($s = readdir($r)){        if($s != '.' && $s != '..'){            // echo $s.'
'; $dir .= $s.'
'; foreach_dir($filename.'/'.$s,$dir); } } return $dir;}echo foreach_dir('templates','');

Output
Default
Subsilverlike

I'm putting echo $s. '
Replaced by $dir. = $s. '
The display path is not complete, where is wrong?

You know the logic of your program, although you pass it in the loop $dir to foreach_dir recursion, but you do not get and deal with the foreach_dir return $dir , so finally you get the root directory and its next level file, there is no further directory.

The program should be like this

function foreach_dir($filename, $dir){    if(!is_dir($filename)) return '';    $r = opendir($filename);    while ($s = readdir($r)) {        if($s != '.' && $s != '..') {            $dir .= $s.'
'; $dir .= foreach_dir($filename . '/' . $s, $dir); } } return $dir;}echo foreach_dir('templates','');

Because you've put the echo out, default and subsilverlike should all be the first level subdirectories under the Templates directory, which is $dir by the statement. = $s. '
' Splicing out, in fact, in addition to the first call function outside nothing is printed, so this is the result. You can change the last sentence of your second piece of code to:

    echo $dir;}foreach_dir('templates','');

You should be able to see more results.

But the logic of the two code is very unclear, I write a paragraph you can see to understand not.

$details = [];function eachDir($dir, &$results) {    if (! is_dir($dir)) return false; //不是目录就不需要处理了    $hd = opendir($dir);    while($file = readdir($hd)) {        if ($file == '.' || $file == '..') continue; //忽略这两个目录        $file = $dir . '/' . $file; //拼接完整的文件名或路径名        $results[] = $file; //放到遍历结果中        if (is_dir($file)) eachDir($file, $results); //如果是目录,递归处理    }    return true;}eachDir('templates', $details); //$details是通过传址的方式处理的print_r($details);/** * 我期望的输出类似 * templates/default * templates/detault/images * ... */

This code is not tested.
It is not recommended to use echo in a function, even if it is practice, because the echo comes out of the things can not be processed, so should try to avoid. At the same time echo will affect your way of thinking, first deal with the output of the time, your attention can be focused on the current steps, while the edge processing edge output is equivalent to two uses.

  • Related Article

    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.