PHP rewinddir() Function

Example

Open a directory, list the files within it, reset the directory handle, list the files within it again, and then close:

<?php
$dir = "/images/";
// Open directory and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    // List files in the images directory again
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    rewinddir();
    // List files in the images directory again
    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

Definition and Usage

The rewinddir() function resets the directory handle created by opendir().

Syntax

rewinddir(dir_handle);
Parameter Description
dir_handle

Optional. Specify the directory handle resource opened previously by opendir().

If this parameter is not specified, the last link opened by opendir() is used.

Technical Details

Return Value: -
PHP Version: 4.0+