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$dir
Passedforeach_dir
But you have not obtained or processedforeach_dir
Returned$dir
So 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.