This article mainly introduces the PHP traversal folder and file list examples, the need for friends can refer to the following
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 code as follows: <?php define (' DS ', directory_separator); Class getdirfile{ //return array private $DirArray = Array (); Privat E $FileArray = Array (); Private $DirFileArray = Array (); Private $Handle, $Dir, $File; //Get directory list Public function Getdir (& $Dir) { Is_dir ($ Dir)) { if (false!= ($Handle = Opendir ($Dir)) { &N Bsp while (false!= ($File = Readdir ($Handle)) { if ($File!= '. ' && $File!= ' ... ' &&!strpos ($File, '. ')) { $DirArray [] = $File; & nbsp }   } Closedir ($Handle); }else{ $ dirarray[] = ' [Path]: '. $Dir. ' isn't a Dir or not found! '; } return $DirArray; } //Get file list Public function GetFile (& $Dir) { &N Bsp if (Is_dir ($Dir)) { if (false!= ($Handle = Opendir ($Dir)) { &NB Sp while (false!= ($File = Readdir ($Handle)) { if ($File!= '. ' && $File!= ' ... ' && strpos ($File, '. ')) { $FileArray [] = $File; } &NBSP ; Closedir ($Handle); }else{ $ filearray[] = ' [Path]: '. $Dir. ' isn't a Dir or not found! '; } return $FileArray; } //Get directory/File list Public function Getdirfile (& $Dir) { &NB Sp if (Is_dir ($Dir)) { $DirFileArray [' dirlist '] = $this->getdir ($Dir); if ($DirFileArray) { for Each ($DirFileArray [' dirlist '] as $Handle) { $File = $Dir. DS. $Handle; $DirFilearray[' filelist ' [$Handle] = $this->getfile ($File); } }else{ $DirFileArray [] = ' [Path]: '. $Dir. ' isn't a Dir or not found! ' ; } return $DirFileArray; } }?> instance: (relative path or absolute path) 1. Get directory list code as follows: <?php $Dir _ Dir = './example '; $getDirFile = new Getdirfile (); $getDir = $getDirFile->getdir ($Dir _dir); Print_r ($getDir);?> display 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 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);?> display code is as follows: Array ( [0] => example.sql [1] = > Example.txt) Array ( [0] => example.php) 3. Get directory/File list code as follows : <?php $Dir _dir = './example '; $getDirFile = new Getdirfile (); $getDirFile = $getDirFile->getdirfile ($Dir _dir); Print_r ($getDirFile);?> display code as follows: Array ( [dirlist] => array ( [0] => example_one [1] => example_tWo ) [filelist] => Array ( & nbsp [Example_one] => Array ( &NB Sp [0] => example.sql &N Bsp [1] => example.txt ) [example_two] => Array ( &NB Sp [0] => example.php &N Bsp ) )