When you are programming in PHP, you need to browse the files under a directory on the server, which is often a directory. The Opendir () function, the Readdir () function, the Closedir () function, and the Rewinddir () function are required to obtain the files and subdirectories in a directory.
① function Opendir ()
function Opendir () is used to open the specified directory, accept the path and directory name of a directory as an argument, and the function return value is a directory handle (resource type) that is available for use by other directory functions. Returns False if the directory does not exist or does not have access rights.
② function Readdir ()
The function Readdir () is used to read the specified directory, accept the operable catalog handle that has been opened with the Opendir () function as a parameter, the function returns a filename for the current directory pointer position, and the directory pointer is moved back one bit. Returns False when the pointer is at the end of the directory because no file exists.
③ function Closedir ()
function Closedir () closes the specified directory and accepts an operable catalog handle that has been opened with the Opendir () function as an argument. function has no return value and closes the Open directory after running.
④ function Rewinddir ()
function Reweinddir () back to the directory handle, accepting an operable catalog handle that has been opened with the Opendir () function as an argument. Resets the directory pointer to the beginning of the directory to the beginning of the directory.
Here is an example of how these functions are used. Note that before using this example, make sure that you have a phpMyAdmin folder under your consent directory. The code looks like this:
Copy Code code as follows:
<?php
$num = 0; Number of subdirectories and files used to count
$dirname = ' phpMyAdmin '; Save a directory name for convenience in the current directory
$dir _handle = Opendir ($dirname); Open the directory with Opendir
Use table format to output the traversed directory and filename
Echo ' <table border= "0" align= "center" width= "cellspacing=" 0 "cellpadding=" 0 ">";
echo ' <caption>Echo ' <tr align= left ' bgcolor= ' #cccccc ' > ';
echo ' <th> filename </th><th> file size </th><th> file type </th><th> modification Time </th> ';
Use the Readdir loop to read content in a directory
while ($file = Readdir ($dir _handle)) {
Connect the files in the directory to the current directory to use in the program
$dirFile = $dirname. " /". $file;
$bgcolor = $num +%2==0? ' #FFFFFF ': ' #CCCCCC '; One color for each line
Echo ' <tr bgcolor= '. $bgcolor. ' > ';
Echo ' <td> '. FileSize ($dirFile). ' </td> '; Show file name
Echo ' <td> '. FileType ($dirFile). ' </td> '; Show file Size
Echo ' <td> '. Date ("y/n/t", Filemtime ($dirFile)). ' </td> '; Format Display file modification time
Echo ' </tr> ';
}
Echo ' </table> ';
Closedir ($dir _handle); Close file Action handle
Echo ' in <b> '. $dirname. ' The subdirectories and files in the </b> directory are in total <b> '. $num. ' </b> a ';
?>
The above program first opens a directory pointer and traverses it. The "." is included when traversing the directory. and ".." Two special directories that you can block if you don't need them. Of course, the display details will vary depending on the contents of the folder. As you can see from the example above, browsing the contents of a folder in PHP is not such a complicated thing. and PHP also provides an object-oriented way for directory traversal, done by using the "Dir" class. Furthermore, PHP can retrieve the contents specified under the directory according to the user's request, and provide the glob () function to retrieve the specified directory. The function eventually returns an array containing the retrieved results.