Summary of usage examples of 4 PHP traversal functions opendir(), readdir(), closedir(), rewinddir()

伊谢尔伦
Release: 2023-03-12 09:02:01
Original
1498 people have browsed it

This article mainly introduces PHPTraverse directoryfunctions opendir(), readdir(), closedir(), rewinddir() summary, and gives a comprehensive use of these The example of the function is a simple file browser. Friends who need it can refer to it

When doing PHP programming, you need to browse the files under a certain directory on the server, which usually becomes a traversal directory. To obtain files and subdirectories in a directory, you need to use the opendir() function, readdir() function, closedir() function and rewinddir() function.

①Function opendir()

Function opendir() is used to open the specified directory. It accepts the path and directory name of a directory as parameters. The function return value is available Directory handle (resource type) used by other directory functions. If the directory does not exist or does not have access rights, returns FALSE.

②Function readdir()

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

③Function closedir()

Function closedir() closes the specified directory and accepts an operable directory handle that has been opened with the opendir() function as a parameter. The function has no return value and the open directory is closed after running.

④Function rewinddir()

The function reweinddir() rewinds the directory handle and accepts an operable directory handle that has been opened with the opendir() function as a parameter. Resets the directory pointer to the beginning of the directory, i.e. rewinds to the beginning of the directory.

The following uses an example to illustrate the use of the above functions. Note, please make sure there is a phpMyAdmin folder in the agreed directory before using this example. The code is as follows:

The code is as follows:

<?php
$num = 0; //用来统计子目录和文件的个数
$dirname = &#39;phpMyAdmin&#39;; //保存当前目录下用来便利的一个目录名
$dir_handle = opendir($dirname); //用opendir打开目录
 
//将遍历的目录和文件名使用表格格式输出
echo &#39;<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">&#39;;
echo &#39;<caption><h2>目录&#39;.$dirname.&#39;下面的内容</h2></caption>&#39;;
echo &#39;<tr align="left" bgcolor="#cccccc">&#39;;
echo &#39;<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th>&#39;;
 
//使用readdir循环读取目录里的内容
while($file = readdir($dir_handle)){
//将将目录下的文件和当前目录连接起来,才能在程序中使用
$dirFile = $dirname."/".$file;
 
$bgcolor = $num+%2==0 ? &#39;#FFFFFF&#39; : &#39;#CCCCCC&#39;; //各行一种颜色
echo &#39;<tr bgcolor=&#39;.$bgcolor.&#39;>&#39;;
echo &#39;<td>&#39;.
filesize
($dirFile).&#39;</td>&#39;; //显示文件名
echo &#39;<td>&#39;.
filetype
($dirFile).&#39;</td>&#39;; //显示文件大小
echo &#39;<td>&#39;.date("Y/n/t",
filemtime
($dirFile)).&#39;</td>&#39;; //格式化显示文件修改时间
echo &#39;</tr>&#39;;
}
 
echo &#39;</table>&#39;; 
closedir($dir_handle); //关闭文件操作句柄
echo &#39;在<b>&#39;.$dirname.&#39;</b>目录下的子目录和文件共有<b>&#39;.$num.&#39;</b>个&#39;;
?>
Copy after login

The above program first opens a directory pointer and traverses it. When traversing the directory, two special directories "." and ".." will be included. If these two directories are not needed, they can be blocked. 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 a folder in PHP is not a complicated matter. Moreover, PHP also provides an object-oriented method for directory traversal, which is completed by using the "dir" class. Not only that, PHP can also retrieve the specified content in the directory according to the user's requirements, and provides the glob() function to retrieve the specified directory. The function ultimately returns an array containing the search results.

The above is the detailed content of Summary of usage examples of 4 PHP traversal functions opendir(), readdir(), closedir(), rewinddir(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template