PHP rewinddir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, rewinddir() resets an open directory handle back to the start of the folder. The next readdir() call then returns the first entry again.

A directory handle remembers where it is. Once readdir() has reached the end, it keeps returning false. Call rewinddir() when you want to read the same folder a second time without closing and re-opening it. It always returns true.

Syntax

Call rewinddir() with the values you need. Start with this line.

PHP
rewinddir($handle);
CallWhat it does
rewinddir($handle)Moves the handle back to the first entry of the folder.
rewinddir($handle); readdir($handle)Resets the handle and then reads the first entry again.
rewinddir()Resets the last opened handle. This is clear only for tiny scripts, so passing the handle is recommended.

rewinddir() does not reload the folder from disk in a way you should depend on, and it does not sort anything. It just puts the reading position back at the beginning. Always pass the handle explicitly.

Library declarations
PHP
rewinddir(?resource $dir_handle = null): true

Parameters

ParameterDescription
dir_handleOptional. The directory handle returned by opendir(). If it is left out or null, PHP uses the last opened handle, but you should always pass the handle yourself.

Return Value

Return ValueDescription
trueAlways returns true. In older PHP versions this function had no useful return value.

PHP Examples

Example 1 — Reading a Folder Twice

We count the files with one handle, rewind it, and count again without calling opendir() a second time.

PHP
<?php
$folder = __DIR__ . '/demo-rewinddir';
mkdir($folder);
file_put_contents($folder . '/a.txt', '1');
file_put_contents($folder . '/b.txt', '2');

$handle = opendir($folder);

$count = 0;
while (($entry = readdir($handle)) !== false) {
    if ($entry !== '.' && $entry !== '..') {
        $count++;
    }
}
echo "First pass: ", $count, PHP_EOL;

rewinddir($handle);

$count = 0;
while (($entry = readdir($handle)) !== false) {
    if ($entry !== '.' && $entry !== '..') {
        $count++;
    }
}
echo "Second pass: ", $count, PHP_EOL;

closedir($handle);
unlink($folder . '/a.txt');
unlink($folder . '/b.txt');
rmdir($folder);

How It Works

  1. The first loop reads the folder until readdir() returns false, and counts two files.
  2. rewinddir($handle) moves the reading position back to the beginning.
  3. The second loop can now read all the entries again with the same handle.
  4. We still close the handle and clean up at the end.

Example 2 — What Happens Without rewinddir()

Once readdir() has reached the end, a second loop finds nothing. This example shows the difference with and without rewinddir().

PHP
<?php
$folder = __DIR__ . '/demo-rewinddir-edge';
mkdir($folder);
file_put_contents($folder . '/a.txt', '1');
file_put_contents($folder . '/b.txt', '2');

$handle = opendir($folder);

while (readdir($handle) !== false) {
    // read everything, just to reach the end
}

$count = 0;
while (($entry = readdir($handle)) !== false) {
    $count++;
}
echo "Without rewinddir: ", $count, PHP_EOL;

rewinddir($handle);

$count = 0;
while (($entry = readdir($handle)) !== false) {
    if ($entry !== '.' && $entry !== '..') {
        $count++;
    }
}
echo "After rewinddir: ", $count, PHP_EOL;

closedir($handle);
unlink($folder . '/a.txt');
unlink($folder . '/b.txt');
rmdir($folder);

How It Works

  1. The first empty-looking loop reads every entry, so the handle is now at the end.
  2. The next loop starts at the end and immediately gets false, so the count stays at 0.
  3. After rewinddir($handle), the same handle starts from the first entry again.
  4. Now the loop finds both files, and the dot entries are skipped.

Example 3 — Rewinding After a Partial Read

You do not have to reach the end before rewinding. We read just one entry, rewind, and then count every entry in the folder.

PHP
<?php
$folder = __DIR__ . '/demo-rewinddir-partial';
mkdir($folder);
file_put_contents($folder . '/a.txt', '1');
file_put_contents($folder . '/b.txt', '2');

$handle = opendir($folder);

readdir($handle);
rewinddir($handle);

$count = 0;
while (($entry = readdir($handle)) !== false) {
    $count++;
}
echo "Entries after rewind: ", $count, PHP_EOL;

closedir($handle);
unlink($folder . '/a.txt');
unlink($folder . '/b.txt');
rmdir($folder);

How It Works

  1. One readdir() call moves the handle forward by a single entry.
  2. rewinddir() puts it back at the start, so that skipped entry is not lost.
  3. This time we count every entry without skipping, so '. ' and '..' are included.
  4. Two files plus the two special entries make 4 in total.

Frequently Asked Questions

It moves an open directory handle back to the start of the folder, so the next readdir() call returns the first entry again.
When you want to loop over the same open folder more than once. Without it, the second loop starts at the end and finds nothing.
It returns true. Older versions of PHP did not return a useful value, so most code does not check the result.
It has a similar effect for reading, but it reuses the same handle. Opening again with opendir() gives you a new handle, which you must also close.
No. After closedir() the handle is no longer valid, and PHP throws a TypeError. Open the folder again with opendir() instead.

Next: scandir()

scandir() returns an array of file and folder names from a path. It is the quickest way to.

scandir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful