Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

rewinddir()

Example

Open a directory, list its files, reset the directory handle, list the files again, and then close the directory.

<?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);
  }
}
?>

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() to the beginning.

Syntax

rewinddir(dir)

Parameter Values

 

Parameter

Description

dir

Optional. Specifies the directory handle resource previously opened with opendir(). If not provided, the function uses the most recently opened directory handle.

Technical Details

Return Value:

Returns NULL on success, or FALSE on failure.

PHP Version:

4.0+