This article mainly introduces the PHP read directory and sub-directory of all the file name method, compared with several common methods, the last example summarizes a PHP read directory and sub-directory of all the file name method, very practical value, the need for friends can refer to, The implementation method is as follows:
In general, PHP read the directory under the file name in a lot of ways, the simplest is scandir, the specific code is as follows:
The code is as follows:
$dir = "./caxa/"; $file =scandir ($dir);p rint_r ($file);
Slightly more complicated, from the PHP manual:
The code is as follows:
$dir = "/etc/php5/";//Open a known directory, and proceed to the read its contentsif (Is_dir ($dir)) {if ($dh = Opendir ($dir)) {while ($file = Readdir ($DH)) {!== false) {echo "filename: $file: filetype:". FileType ($dir. $file). "\ n";} Closedir ($DH);}}
These can only read files in the currently specified directory, and files in subdirectories cannot be read. Originally wrote a loop to delete all directories of a piece of code, you need to delete all files, including multi-level subdirectories. But only need to read out the file name, a little more complex, the Internet to find a can use, the original code has error hints, changed the reference & $data place, as follows:
The code is as follows:
function Searchdir ($path,& $data) {if (Is_dir ($path)) {$DP =dir ($path), while ($file = $DP->read ()) {if ($file! = '). && $file! = ' ... ') {Searchdir ($path. ') /'. $file, $data);}} $DP->close ();} if (Is_file ($path)) {$data []= $path;}} function Getdir ($dir) {$data =array (); Searchdir ($dir, $data); return $data;} Print_r (Getdir ('. '));