PHP traverses directory functions opendir of readdir of closedir of rewinddir of summary

  • 2021-08-03 09:34:46
  • OfStack

In PHP programming, you need to browse the files under a directory of the server, which is usually a traversal directory. To get files and subdirectories under a directory, you need to use opendir (), readdir (), closedir () and rewinddir () functions.

① Function opendir ()

The function opendir () is used to open the specified directory, and takes the path and directory name of 1 directory as parameters. The return value of the function is the directory handle (resource type) that can be used by other directory functions. If the directory does not exist or has no access rights, FALSE is returned.

② Function readdir ()

The function readdir () reads the specified directory, accepts as an argument an operable directory handle that has been opened with the opendir () function, returns a filename of the current directory pointer position, and moves the directory pointer one bit backward. When the pointer is at the end of the directory, FALSE is returned because no file exists.

③ Function closedir ()

The function closedir () closes the specified directory, taking as an argument the handle to the operable directory that has been opened with the function opendir (). Function has no return value, and closes the open directory after running.

④ Function rewinddir ()

The function reweinddir () reverses the directory handle, taking as an argument the operable directory handle that has been opened with the opendir () function. Resets the directory pointer to the beginning of the directory, that is, rewinds the beginning of the directory.

Here is an example to illustrate the use of the above functions. Note that before using this example, make sure that there is an phpMyAdmin folder under the consent directory. The code looks like this:


<?php
$num = 0; // Used to count the number of subdirectories and files
$dirname = 'phpMyAdmin'; // Save the current directory for convenience 1 Directory names
$dir_handle = opendir($dirname); // Use opendir Open Directory
 
// Output the traversed directory and file name in tabular format
echo '<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">';
echo '<caption><h2> Directory '.$dirname.' The following contents </h2></caption>';
echo '<tr align="left" bgcolor="#cccccc">';
echo '<th> Filename </th><th> File size </th><th> File type </th><th> Modification time </th>';
 
// Use readdir Loop to read the contents of the directory
while($file = readdir($dir_handle)){
// The files in the directory will be connected with the current directory to be used in the program
$dirFile = $dirname."/".$file;
 
$bgcolor = $num+%2==0 ? '#FFFFFF' : '#CCCCCC'; // Rows 1 Colors
echo '<tr bgcolor='.$bgcolor.'>';
echo '<td>'.filesize($dirFile).'</td>'; // Display file name
echo '<td>'.filetype($dirFile).'</td>'; // Display file size
echo '<td>'.date("Y/n/t",filemtime($dirFile)).'</td>'; // Formatted display file modification time
echo '</tr>';
}
 
echo '</table>';
closedir($dir_handle); // Close file action handle
echo ' In <b>'.$dirname.'</b> Subdirectories and files under the directory have a total of <b>'.$num.'</b> A ';
?>

The above program first opens a directory pointer and traverses it. When traversing directories, two special directories, "." and "..", are included, which can be masked if they are not needed. Of course, the display details will vary depending on the contents of the folder. As can be seen from the above example, browsing the contents of folders in PHP is not a complicated matter. Moreover, PHP also provides an object-oriented way to traverse directories, which is completed by using the "dir" class. Moreover, PHP can also retrieve the contents specified in the directory according to the user's requirements, and provides glob () function to retrieve the specified directory. The function eventually returns an array containing the retrieval results.


Related articles: