PHP rewinddir() Function

P
PHP mail() Function
PHP scandir() Function

In this article, you will learn how to reset the directory handle in PHP. The rewinddir() function in PHP resets the directory handle created by opendir(). It clears all the options applied to the handle and return to its very first state.

what is the syntax of the REWINDDIR() function in php?

rewinddir(dir)
ParameterDescription
dirOptional. Specifies the directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed
PHP rewinddir() method

examples of the REWINDDIR() function

Example 1. In this example, we open a directory, list its files, reset directory handle, list its files once again, then close.

<?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);
  }
}
?>
PHP mail() Function
PHP scandir() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z