This article mainly introduces examples of php traversal folders and file lists. you can refer to the following example to write a simple class for PHP directory traversal and file list, and attach the example for use, for more information, see.
The code is as follows:
Define ('Ds', DIRECTORY_SEPARATOR );
Class getDirFile {
// Returns an array.
Private $ DirArray = array ();
Private $ FileArray = array ();
Private $ DirFileArray = array ();
Private $ Handle, $ Dir, $ File;
// Obtain the directory list
Public function getDir (& $ Dir ){
If (is_dir ($ Dir )){
If (false! = ($ Handle = opendir ($ Dir ))){
While (false! = ($ File = readdir ($ Handle ))){
If ($ File! = '.' & $ File! = '..'&&! Strpos ($ File ,'.')){
$ DirArray [] = $ File;
}
}
Closedir ($ Handle );
}
} Else {
$ DirArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ DirArray;
}
// Obtain the file list
Public function getFile (& $ Dir ){
If (is_dir ($ Dir )){
If (false! = ($ Handle = opendir ($ Dir ))){
While (false! = ($ File = readdir ($ Handle ))){
If ($ File! = '.' & $ File! = '..' & Strpos ($ File ,'.')){
$ FileArray [] = $ File;
}
}
Closedir ($ Handle );
}
} Else {
$ FileArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ FileArray;
}
// Obtain the directory/file list
Public function getDirFile (& $ Dir ){
If (is_dir ($ Dir )){
$ DirFileArray ['dirlist'] = $ this-> getDir ($ Dir );
If ($ DirFileArray ){
Foreach ($ DirFileArray ['dirlist'] as $ Handle ){
$ File = $ Dir. DS. $ Handle;
$ DirFileArray ['filelist'] [$ Handle] = $ this-> getFile ($ File );
}
}
} Else {
$ DirFileArray [] = '[Path]: \ ''. $ Dir.' \ 'is not a dir or not found! ';
}
Return $ DirFileArray;
}
}
?>
Instance: (relative or absolute path)
1. get the directory list
The code is as follows:
$ Dir_dir = './example ';
$ GetDirFile = new getDirFile ();
$ GetDir = $ getDirFile-> getDir ($ Dir_dir );
Print_r ($ getDir );
?>
Display
The code is as follows:
$ File_one_dir = './example/example_one ';
$ File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two ';
$ GetDirFile = new getDirFile ();
$ GetFile_one = $ getDirFile-> getFile ($ File_one_dir );
$ GetFile_two = $ getDirFile-> getFile ($ File_two_dir );
Print_r ($ getFile_one );
Print_r ($ getFile_two );
?>
2. get the file list
The code is as follows:
$ File_one_dir = './example/example_one ';
$ File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two ';
$ GetDirFile = new getDirFile ();
$ GetFile_one = $ getDirFile-> getFile ($ File_one_dir );
$ GetFile_two = $ getDirFile-> getFile ($ File_two_dir );
Print_r ($ getFile_one );
Print_r ($ getFile_two );
?>
Display
The code is as follows:
Array
(
[0] => example. SQL
[1] => example.txt
)
Array
(
[0] => example. php
)
3. get the directory/file list
The code is as follows:
$ Dir_dir = './example ';
$ GetDirFile = new getDirFile ();
$ GetDirFile = $ getDirFile-> getDirFile ($ Dir_dir );
Print_r ($ getDirFile );
?>
Display
The code is as follows:
Array
(
[DirList] => Array
(
[0] => example_one
[1] => example_two
)
[FileList] => Array
(
[Example_one] => Array
(
[0] => example. SQL
[1] => example.txt
)
[Example_two] => Array
(
[0] => example. php
)
)
)