PHP Function Reference

PHP rewinddir() Function



The PHP rewinddir() function resets the directory stream indicated by dir_handle to the beginning of the directory. The stream must have previously been opened by opendir() function.

Syntax

rewinddir(dir_handle)

Parameters

dir_handle Optional. Specify the directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed.

Return Value

Returns null on success or false on failure.

Example: rewinddir() example

The example below shows the usage of rewinddir() function.

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

//opens the directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    
    //reading all entry from directory handle
    while (($file = readdir($dh)) !== false){
      echo "File Name: ".$file."\n";
    }
    
    //resetting the directory stream to the 
    //beginning of the directory
    rewinddir();

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

    //closing the directory handle
    closedir($dh);
  }
}
?>

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 Directory Reference