Can you tell me where the recursive folder function is wrong?

Source: Internet
Author: User
Subsilverlike. If this is the case, {code...} Will output defaultsubsilver...
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','');

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

If this is the case

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

Here is echo $ s .'
'; Changed to $ dir. = $ s .'
'; The display path is incomplete. What's 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','');

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

If this is the case

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

Here is echo $ s .'
'; Changed to $ dir. = $ s .'
'; The display path is incomplete. What's wrong?

After you read the logic of your program, you can see that although you$dirPassedforeach_dirBut you have not obtained or processedforeach_dirReturned$dirSo what you get at the end is the root directory and its next-level file. There is no more 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 have injected all the echo, the default and subsilverlike should be the first sub-directories under the templates Directory, which is composed of the statement $ dir. = $ s .'
'. In fact, nothing except the first call of the function is printed, so this is the result. You can modify the last few sentences of your second code:

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

We should be able to see more results.

However, the logic of the two sections of code is not clear. I will write a paragraph to see if you can understand it.

$ Details = []; function eachDir ($ dir, & $ results) {if (! Is_dir ($ dir) return false; // you do not need to process the $ hd = opendir ($ dir); while ($ file = readdir ($ hd )) {if ($ file = '. '| $ file = '.. ') continue; // ignore these two directories $ file = $ dir. '/'. $ file; // concatenate the complete file name or path name $ results [] = $ file; // Put It In The traversal result if (is_dir ($ file) eachDir ($ file, $ results); // if it is a directory, recursive processing} return true;} eachDir ('templates', $ details ); // $ details is the print_r ($ details) that is processed by addressing ); /*** the output I expect is similar to * templates/default * templates/detault/images *... */

This code is not tested.
We do not recommend that you use echo in functions. Even in practice, the echo output cannot be processed, so avoid it as much as possible. At the same time, echo will also affect your way of thinking. when processing and then outputting, your attention can be focused on the current step, while processing and outputting are equivalent to one-of-the-art.

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.