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);
Call
What 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
Parameter
Description
dir_handle
Optional. 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 Value
Description
true
Always 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.