Written a simple class for PHP to traverse directories and file lists, and attached usage examples, please refer to it
class getDirFile{
//Return array
private $DirArray = array();
private $FileArray = array();
private $DirFileArray = array();
private $Handle,$Dir,$File;
//Get directory list
public function getDir( & $Dir ){
if( is_dir($Dir) ){
if( false != ($Handle = opendir($Dir)) ; '.') ){
$DirArray[] = $File; ( $Handle );
] = '[Path]:''.$Dir.'' is not a dir or not found!';
}
return $DirArray;
}
//Get the file list
public function getFile( & $Dir ){
if( is_dir($Dir) ){
if( false != ($Handle = opendir($Dir)) ; .') ){
$FileArray[] = $File; ( $Handle );
= '[Path]:''.$Dir.'' is not a dir or not found!';
return $FileArray;
}
//Get directory/file list
public function getDirFile( & $Dir ){
if( is_dir($Dir) ){
$DirFileArray['DirList'] = $this-> getDir( $Dir );
$ DirFileArray['FileList'][$Handle] = $this->getFile( $File ); '[Path]:' '.$Dir.'' is not a dir or not found!';
}
return $DirFileArray;
}
}
?>
Example: (relative path or absolute path)
1. Get the directory list
Copy the code
The code is as follows:
$Dir_dir = './example';
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );
?>
显示
$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.获取文件列表
$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);
?>
显示
Array
(
[0] => example.php
)
3.获取目录/文件列表
$getDirFile = new getDirFile();
$getDirFile = $getDirFile->getDirFile( $Dir_dir );
print_r($getDirFile);
?>
显示
[FileList] => Array
(
[example_one] => Array
(
[0] => example.sql
[1] => example.txt
)
[example_two] => Array
(
[0] => example.php
)
)
)