For PHP to traverse the directory and file list to write a simple class, and attach the use of examples, we refer to the use of it
Copy the Code code as follows:
Define (' DS ', directory_separator);
Class getdirfile{
Returns an 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 not found! ';
}
return $DirArray;
}
Get a list of files
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;
}
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 not found! ';
}
return $DirFileArray;
}
}
?>
Instance: (relative path or absolute path)
1. Get a list of directories
Copy the Code code as follows:
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDir = $getDirFile->getdir ($Dir _dir);
Print_r ($getDir);
?>
Show
Copy the Code code 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 a list of files
Copy the Code code 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);
?>
Show
Copy the Code code as follows:
Array
(
[0] = Example.sql
[1] = Example.txt
)
Array
(
[0] = example.php
)
3. Get a list of directories/files
Copy the Code code as follows:
$Dir _dir = './example ';
$getDirFile = new Getdirfile ();
$getDirFile = $getDirFile->getdirfile ($Dir _dir);
Print_r ($getDirFile);
?>
Show
Copy the 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
)
)
)
http://www.bkjia.com/PHPjc/740203.html www.bkjia.com true http://www.bkjia.com/PHPjc/740203.html techarticle for PHP to traverse the directory and file list to write a simple class, and attach the use of examples, we refer to the use of the copy code is as follows:? PHP define (' DS ', directory_separator), Class G ...