PHP Function Reference

PHP Directory - rewind() Method



The PHP Directory::rewind() method resets the directory stream to the beginning of the directory.

This method is same as rewinddir() function, only dir_handle defaults to $this->handle.

Syntax

public Directory::rewind()

Parameters

No parameter is required.

Return Value

Returns null on success or false on failure.

Example: Directory::rewind() example

The example below shows the usage of Directory::rewind() method.

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

//opens the directory, and read its contents
if (is_dir($dir)){
  if ($d = dir($dir)){
    
    //reading all entry from directory handle
    while (($file = $d->read()) !== false){
      echo "File Name: ".$file."\n";
    }

    //resetting the directory stream to the 
    //beginning of the directory
    $d->rewind();

    //reading all entry once again
    while (($file = $d->read()) !== false){
      echo "File Name: ".$file."\n";
    } 

    //closing the directory handle
    $d->close();
  }
}
?>

The output of the above code will be:

File Name: fig1.png
File Name: fig2.png
File Name: Photo.jpg
File Name: .
File Name: ..
File Name: error.jpeg
File Name: fig1.png
File Name: fig2.png
File Name: Photo.jpg
File Name: .
File Name: ..
File Name: error.jpeg

❮ PHP Date and Time Reference