rewinddir

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

rewinddir倒回目录句柄

说明

rewinddir ( resource $dir_handle ) : void

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

参数

dir_handle

目录句柄的 resource,之前由 opendir() 打开

User Contributed Notes

ASchmidt at Anamera dot net 11-Jun-2018 07:59
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.
osamahussain897 at gmail dot com 27-Jan-2018 09:08
/* 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