For PHP traversal directory and file list wrote a simple class, and attached to the use of examples, we refer to the use of the bar
Copy Code code as follows:
<?php
Define (' DS ', directory_separator);
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))) {
while (false!= ($File = Readdir ($Handle))) {
if ($File!= '. ' && $File!= ' ... ' &&!strpos ($File, '. ')) {
$DirArray [] = $File;
}
}
Closedir ($Handle);
}
}else{
$DirArray [] = ' [path]:\ '. $Dir. ' is not ' a Dir ' or ' found! ';
}
return $DirArray;
}
Get 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 ' found! ';
}
return $FileArray;
}
Get 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 ' found! ';
}
return $DirFileArray;
}
}
?>
Instance: (relative path or absolute path)
1. Get directory List
Copy Code code as follows:
<?php
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDir = $getDirFile->getdir ($Dir _dir);
Print_r ($getDir);
?>
Show
Copy Code code 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 file List
Copy Code code 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);
?>
Show
Copy Code code as follows:
Array
(
[0] => Example.sql
[1] => Example.txt
)
Array
(
[0] => example.php
)
3. Get Directory/File list
Copy Code code as follows:
<?php
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDirFile = $getDirFile->getdirfile ($Dir _dir);
Print_r ($getDirFile);
?>
Show
Copy Code code 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
)
)
)