PHP Tutorial - PHP rewinddir() Function






Definition

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

Syntax

PHP rewinddir() Function has the following syntax.

rewinddir(dir_handle);

Parameter

ParameterIs RequiredDescription
dir_handleOptional.Directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed

Example

Open a directory, list its files, reset directory handle, list its files once again, then close:


<?php/* w w  w  . ja  v  a 2s  . c o  m*/
$dir = "/images/";
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    // List files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "\n";
    }
    rewinddir();
    // List once again files in images directory
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "\n";
    }
    closedir($dh);
  }
}
?>