PHP custom Traverse directory All Files dir (), Readdir () function
Method One: Use Dir () to traverse the directory
Dir () function, returns the Directory class instance when successful
The syntax format for the PHP dir () is:
Dir (directory);//directory is a directory name that needs to display the file name, which can contain path information
PHP dir () Usage example: Lists all filenames under the upload directory:
The code is as follows |
|
$dir = @ Dir ("upload");//open upload directory; "@" to block error messages, because sometimes there is no file in the directory where the files need to be displayed, you may report an error and hide the error with "@" Enumerate all files in the upload directory while (($file = $dir->read ())!== false) { echo "File name:". $file. " "; } $dir->close (); ?> |
The output is:
Filename:.
Filename:..
File name: logo.gif
File name: arrow.gif
File name: bg.gif
Example
The code is as follows |
|
function tree ($dir) { $mydir = Dir ($dir); while ($file = $mydir->read ()) { if ($file! = '. ' && $file! = ' ... ') { if (Is_dir ("$dir/$file")) { Echo ' Directory name: '. $dir. Directory_separator. " $file. ' '; Tree ("$dir/$file"); }else{ echo ' filename: '. $dir. Directory_separator. $file. ' '; } } } $mydir->close (); } Tree ('./phpmyadmin '); |
Method two uses Readir () to traverse the directory
Definition and usage
The Readdir () function returns an entry in a directory handle opened by Opendir ().
If successful, the function returns a file name, otherwise it returns false.
Grammar
Readdir (Dir_stream)
Example
The code is as follows |
|
Header (' Content-type:text/html;charset=utf-8 ');
function Listdir ($dir) { if (Is_dir ($dir)) { if ($handle = Opendir ($dir)) { while ($file = Readdir ($handle)) { if ($file! = '. ' && $file! = ' ... ') { if (Is_dir ($dir. Directory_separator. $file)) { Echo ' Directory name: '. $dir. Directory_separator. " $file. ' '; Listdir ($dir. Directory_separator. $file); }else{ echo ' filename: '. $dir. Directory_separator. $file. ' '; } } } } Closedir ($handle); }else{ echo ' Non-valid catalog! '; } } Listdir ('./phpmyadmin '); |
http://www.bkjia.com/PHPjc/834970.html www.bkjia.com true http://www.bkjia.com/PHPjc/834970.html techarticle PHP custom Traverse directory All Files dir (), Readdir () function method one: Use Dir () to traverse the directory Dir () function, the successful return directory class instance PHP dir () syntax format is: Dir (di ...