rewinddir

(PHP 4, PHP 5, PHP 7, PHP 8)

rewinddir倒回目录句柄

说明

rewinddir(?resource $dir_handle = null): void

dir_handle 指定的目录流重置到目录的开头。

参数

dir_handle
先前通过 opendir() 打开的目录句柄 resource。如果 dir_handlenull,则使用最近一次通过 opendir() 打开的句柄。

返回值

没有返回值。

更新日志

版本 说明
8.5.0 现在已弃用将 dir_handle 设为 null 的做法,应明确提供最近打开的目录句柄。
8.0.0 现在 dir_handle 现在允许为 null。

示例

完整示例请参见 opendir() 的文档。

参见

  • opendir() - 打开目录句柄
  • readdir() - 从目录句柄中读取条目
  • closedir() - 关闭目录句柄
  • dir() - 返回一个 Directory 类实例
  • is_dir() - 判断给定文件名是否是一个目录
  • glob() - 寻找与模式匹配的文件路径
  • scandir() - 列出指定路径中的文件和目录
添加备注

用户贡献的备注 2 notes

up
6
ASchmidt at Anamera dot net
7 years ago
It is crucial to note that rewinddir() does not simply start over at the beginning of the SAME directory list. Instead, this function first re-reads the directory - thus any file that were deleted (or inserted) since the original opendir() will be reflected after "rewinding".

In that respect, rewinddir() is equivalent to a closedir(), opendir() sequence, but without obtaining a new handle.
up
6
osamahussain897 at gmail dot com
7 years ago
/* Source Code */

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    // List files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    rewinddir();
    // List once again files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>

/* Result */

filename: cat.gif
filename: dog.gif
filename: horse.gif
filename: cat.gif
filename: dog.gif
filename: horse.gif
To Top