PHP: rewinddir - Manual
(PHP 4, PHP 5, PHP 7, PHP 8)
rewinddir — Rewind directory handle
Description
Resets the directory stream indicated by dir_handle
to the beginning of the directory.
Parameters
Return Values
No value is returned.
Changelog
| Version | Description |
|---|---|
| 8.5.0 |
Using null for dir_handle is now deprecated.
Instead, the last opened directory handle should be explicitly provided.
|
| 8.0.0 |
dir_handle is now nullable.
|
Examples
For a complete example refer to the opendir() documentation.
See Also
- opendir() - Open directory handle
- readdir() - Read entry from directory handle
- closedir() - Close directory handle
- dir() - Return an instance of the Directory class
- is_dir() - Tells whether the filename is a directory
- glob() - Find pathnames matching a pattern
- scandir() - List files and directories inside the specified path
Found A Problem?
osamahussain897 at gmail dot com ¶
8 years ago
/* Source Code */
<?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.gif8 years ago
It is crucial to note that rewinddir() does not simply start over at the beginning of the SAME directory list. Instead, this function first re-reads the directory - thus any file that were deleted (or inserted) since the original opendir() will be reflected after "rewinding".
In that respect, rewinddir() is equivalent to a closedir(), opendir() sequence, but without obtaining a new handle.