W3docs

rewinddir()

Learn how PHP's rewinddir() resets a directory stream's internal pointer to the start so you can re-read its contents without reopening the handle.

When you read a directory in PHP, an internal pointer tracks how far you have gotten. Once you reach the end, that pointer stays there — call readdir() again and you get false with no entries. rewinddir() moves the pointer back to the first entry so you can read the directory a second time without reopening it. This page covers what the function does, its syntax, a working multi-pass example, common pitfalls, and when (and when not) to reach for it.

Syntax

rewinddir(?resource $dir_handle = null): void
  • $dir_handle — a directory handle previously returned by opendir(). If omitted, PHP uses the handle from the most recent opendir() call.
  • Return valuerewinddir() returns void (in older PHP versions, null). It does not signal success or failure; if you pass an invalid handle, PHP raises a TypeError or warning instead.

It is almost always paired with opendir(), readdir(), and closedir().

How rewinddir() works

A directory handle behaves like a cursor over the directory's entries. readdir() advances that cursor one entry per call and returns false when it runs out. rewinddir() simply repositions the cursor at the beginning so the next readdir() starts over from the first entry.

Reading a directory twice

This is the canonical use case: list a directory, then list it again from the same handle. Note how readdir($h) returns no entries on the second pass until rewinddir() is called.

<?php
$h = opendir(__DIR__);
if ($h === false) {
    die("Failed to open directory\n");
}

echo "First pass:\n";
while (($file = readdir($h)) !== false) {
    echo "  $file\n";
}

// Without rewinddir(), this loop would print nothing.
rewinddir($h);

echo "Second pass:\n";
while (($file = readdir($h)) !== false) {
    echo "  $file\n";
}

closedir($h);
?>

Both passes print the same set of entries (including the . and .. pseudo-directories that readdir() always returns).

A common gotcha: the strict false check

Always compare the result of readdir() with !== false, not just != or a truthy test. A directory can legitimately contain an entry named "0", which is falsy in PHP — a loose check would stop the loop early and silently skip files.

<?php
// Wrong: stops as soon as it hits a file named "0" (or an empty name)
while ($file = readdir($h)) { /* ... */ }

// Correct: only stops at the genuine end of the stream
while (($file = readdir($h)) !== false) { /* ... */ }
?>

When to use rewinddir() — and when not to

Use rewinddir() when you need more than one pass over the same open handle, for example to compute a total in the first pass (count files, sum sizes) and act on each entry in the second. It is cheaper than calling closedir() and opendir() again because it avoids re-opening the underlying file descriptor.

If you only need the entries once, or you want them sorted, prefer scandir(), which returns the whole listing as an array in a single call — no pointer to manage. For an overview of all directory functions, see the PHP Directory chapter.

Conclusion

rewinddir() resets a directory stream's internal pointer to the start, letting you re-read the directory without the overhead of closing and reopening the handle. Paired with opendir(), readdir(), and closedir(), it makes multi-pass directory traversal simple and efficient. Reach for scandir() instead when a single sorted listing is all you need.

graph TD
    A[opendir] --> B[readdir]
    B --> C{More items?}
    C -->|Yes| B
    C -->|No| D[rewinddir]
    D --> B
    B --> E[closedir]

Practice

Practice
What is the primary function of the 'rewinddir()' function in PHP?
What is the primary function of the 'rewinddir()' function in PHP?
Was this page helpful?