Phpreaddir function usage tutorial
- $ Dir = "readdir /";
- // Determine whether it is a directory
- If (is_dir ($ dir )){
- If ($ dh = opendir ($ dir )){
- While ($ file = readdir ($ dh ))! = False ){
- Echo "filename: $ file: filetype:". filetype ($ dir. $ file )."";
- }
- Closedir ($ dh );
- }
- }
Example 2: note that the error message does not exist before 4.0.0-RC2! = Operator.
- If ($ handle = opendir ('/path/to/Files ')){
- Echo "Directory handle: $ handle ";
- Echo "Files :";
- /* This is the correct way to traverse directories */
- While (false! ==( $ File = readdir ($ handle ))){
- Echo "$ file ";
- }
- /* This is the method for mistakenly traversing directories */
- While ($ file = readdir ($ handle )){
- Echo "$ file ";
- }
- Closedir ($ handle );
- }
In example 3, readdir () will return the ".." and ".." entries. if you do not want to use them, filter them out. in example 2, list all the files in the current directory and remove...
- If ($ handle = opendir ('.')){
- While (false! ==( $ File = readdir ($ handle ))){
- If ($ file! = "." & $ File! = ".."){
- Echo "$ file ";
- }
- }
- Closedir ($ handle );
- }
Note: readdir must be used with opendir. |