I wrote a simple class for PHP to traverse directories and file lists, and attached the example for your reference.
Copy codeThe Code is as follows:
<? Php
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
Copy codeThe Code is as follows:
<? Php
$ Dir_dir = './example ';
$ GetDirFile = new getDirFile ();
$ GetDir = $ getDirFile-> getDir ($ Dir_dir );
Print_r ($ getDir );
?>
Display
Copy codeThe Code is as follows:
<? Php
$ 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
Copy codeThe Code is as follows:
<? Php
$ 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
Copy codeThe Code is as follows:
Array
(
[0] => example. SQL
[1] => example.txt
)
Array
(
[0] => example. php
)
3. Get the directory/file list
Copy codeThe Code is as follows:
<? Php
$ Dir_dir = './example ';
$ GetDirFile = new getDirFile ();
$ GetDirFile = $ getDirFile-> getDirFile ($ Dir_dir );
Print_r ($ getDirFile );
?>
Display
Copy codeThe 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
)
)
)