Please help me see the differences between the two codes. Why are the output results different /**
* Traverse directories and subdirectories and files
* @ Param string $ dir
* @ Param int $ level
*
*/
Function scan_dir ($ dir = ".", $ level = 0 ){
// Determine whether the current path is a file. If yes, return directly.
Static $ arr = array ();
If (! Is_dir ($ dir )){
$ Arr [] = array (
'File' => $ dir,
'Level' => $ level,
'Type' => 'file'
);
// Echo ''. $ dir .''.'
';
Return;
}
// 2 open the directory resource
$ Resorc = opendir ($ dir );
If (! $ Resorc ){
Exit ('directory opening failed ');
}
// 3. traverse the directory cyclically
While ($ file = readdir ($ resorc )){
// Var_dump ($ file); die ();
// Exclude... and ..
If ($ file = '.' | $ file = '..'){
// Do nothing
} Else {
// Otherwise, it is a file or folder.
// Form a complete path
$ Path = $ dir. '/'. $ file;
// Var_dump ($ path); exit ();
// Determine whether it is a file
If (is_file ($ path )){
Var_dump ($ path); echo '1
';
// Output file
$ Arr [] = array (
'File' => $ file,
'Level' => $ level,
'Type' => 'file'
);
// Echo str_repeat ("", $ level). ''. $ file .''.'
';
} Else {
// Output folder name
$ Arr [] = array (
'Dir' => $ dir,
'Level' => $ level,
'Type' => 'dir'
);
// Echo str_repeat ("", $ level). ''. $ dir .''.'
';
// Otherwise, it is a directory and then recursively calls the function to traverse the directory Currently traversed.
Scan_dir ($ path, $ level + 1 );
}
}
}
Return $ arr;
}
$ Arr = scan_dir (); echo'
';
// Var_dump ($ arr );
Foreach ($ arr as $ value ){
If ($ value ['type'] = 'file '){
// Var_dump ($ value ['file']);
Echo str_repeat ("", $ value ['level']). '. $ value ['file'].'
';
} Else {
Echo str_repeat ("", $ value ['level']). '. $ value ['dir'].'
';
}
}
The output result is as follows:
Is the file name and directory name of the current directory and subdirectory
Digui. php
.
A. php
./Sbuscan // The subc directory is not output, but the subc. php file under the subc directory is output.
Subc. php
Test.txt
See the following code.
Function myScandir ($ dir = ".", $ level = 0 ){
// Save all the objects and folders that have been traversed. you must be able to differentiate parent-child relationships.
Static $ arr = array ();
// 1. check whether the current path is a directory. if it is not a directory, the current file is directly returned.
If (! Is_dir ($ dir )){
// Output the current file in blue
// Echo ''. $ dir .'
';
// To distinguish the layer of the current file, use $ level to record
$ Arr [] = array ('level' => $ level, 'filename' => $ dir, 'type' => 'file ');
// Stop execution
// Return is not necessarily required
Return;
}
// 2. get Directory resources
$ O = opendir ($ dir );
// Handle directory opening failure
If (! $ O ){
Exit ('directory opening failed ');
}
// 3. traverse the directory cyclically
While ($ filename = readdir ($ o )){
// Remove the two folders:. and.
If ($ filename = '.' | $ filename = '..'){
// Do nothing
} Else {
// Core code
// A normal File: either a folder or a file
// 20140622/son
// Create a complete path for the file Currently traversed
$ File_dir = $ dir. '/'. $ filename;
// Determine the file type
If (is_file ($ file_dir )){
// Var_dump ($ file_dir); echo '2
';
// A File
// Use str_repeat () to repeatedly output multiple spaces
// Echo str_repeat ("", $ level), ''. $ filename .'
';
$ Arr [] = array ('level' => $ level, 'filename' => $ filename, 'type' => 'file ');
} Else {
// Output the name of the current folder
// Echo str_repeat ("", $ level), ''. $ filename .'
';
$ Arr [] = array ('level' => $ level, 'filename' => $ filename, 'type' => 'dir ');
// It is a file path, which requires in-depth traversal.
// Call yourself recursively and change the path to the directory Currently traversed
MyScandir ($ file_dir, $ level + 1 );
// Var_dump ($ file_dir );
}
}
}
// Return the final Retrieved array
Return $ arr;
}
// Return is not necessarily related to whether to use a variable to receive a function call.
// If the function does not return, variable receiving cannot be defined (meaningless)
$ Arr = myScandir ();
// Traverse the array and output it in blue according to the file. The red array of the folder clearly shows the parent-child relationship.
Foreach ($ arr as $ value ){
// Each $ value represents a file. which level does it belong to? is it a file or a folder?
If ($ value ['type'] = 'dir '){
// The current file is a folder
Echo str_repeat ("", $ value ['level']), '. $ value ['filename'].'
';
} Else {
// A File
Echo str_repeat ("", $ value ['level']), '. $ value ['filename'].'
';
}
}
The output of this code is as follows:
Digui. php
Sbuscan
A. php
Sbuc // Here, the subc directory outputs why the above code is not output.
Subc. php
Test.txt
Reply to discussion (solution)
Add debugging information
// Determine whether it is a file if (is_file ($ path) {// var_dump ($ path); echo '1
'; // Output file $ arr [] = array ('path' => $ path, 'file' => $ file, 'level' => $ level, 'type' => 'file'); // echo str_repeat ("", $ level ). ''. $ file. ''.'
';} Else {// name of the output folder $ arr [] = array ('path' => $ path, 'dir' => $ dir, 'level' => $ level, 'type' => 'dir'); // echo str_repeat ("", $ level ). ''. $ dir. ''.'
'; // Otherwise, it is a directory and then recursively calls the function to traverse the directory scan_dir ($ path, $ level + 1) that is currently traversed in depth );}
Printr ($ arr); then you can see the problem.
Add debugging information
// Determine whether it is a file if (is_file ($ path) {// var_dump ($ path); echo '1
'; // Output file $ arr [] = array ('path' => $ path, 'file' => $ file, 'level' => $ level, 'type' => 'file'); // echo str_repeat ("", $ level ). ''. $ file. ''.'
';} Else {// name of the output folder $ arr [] = array ('path' => $ path, 'dir' => $ dir, 'level' => $ level, 'type' => 'dir'); // echo str_repeat ("", $ level ). ''. $ dir. ''.'
'; // Otherwise, it is a directory and then recursively calls the function to traverse the directory scan_dir ($ path, $ level + 1) that is currently traversed in depth );}
Printr ($ arr); then you can see the problem.
Add debugging information
// Determine whether it is a file if (is_file ($ path) {// var_dump ($ path); echo '1
'; // Output file $ arr [] = array ('path' => $ path, 'file' => $ file, 'level' => $ level, 'type' => 'file'); // echo str_repeat ("", $ level ). ''. $ file. ''.'
';} Else {// name of the output folder $ arr [] = array ('path' => $ path, 'dir' => $ dir, 'level' => $ level, 'type' => 'dir'); // echo str_repeat ("", $ level ). ''. $ dir. ''.'
'; // Otherwise, it is a directory and then recursively calls the function to traverse the directory scan_dir ($ path, $ level + 1) that is currently traversed in depth );}
Printr ($ arr); then you can see the problem.
When the problem is found, the level [level] is a bit messy, but I don't know how to solve it. please advise. Thank you!
Output:
Array
(
[0] => Array
(
[Path] =>./digui. php
[File] => digui. php
[Level] => 0
[Type] => file
)
[1] => Array
(
[Path] =>./sbuscan
[Dir] =>.
[Level] => 0
[Type] => dir
)
[2] => Array
(
[Path] =>./sbuscan/a. php
[File] => a. php
[Level] => 1
[Type] => file
)
[3] => Array
(
[Path] =>./sbuscan/sbuc
[Dir] =>./sbuscan
[Level] => 1
[Type] => dir
)
[4] => Array
(
[Path] =>./sbuscan/sbuc/subc. php
[File] => subc. php
[Level] => 2
[Type] => file
)
[5] => Array
(
[Path] =>./test.txt
[File] => test.txt
[Level] => 0
[Type] => file
)
[6] => Array
(
[Path] =>./tiaoshi. php
[File] => tiaoshi. php
[Level] => 0
[Type] => file
)
)