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.