Backend Development 2 min read

PHP rewinddir() Function – Reset Directory Handle to the Beginning

The PHP rewinddir() function resets a directory handle opened by opendir() back to the start of the directory stream, returning the handle on success or FALSE on failure, and is illustrated with a complete example that reads, rewinds, and reads the directory contents again.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP rewinddir() Function – Reset Directory Handle to the Beginning

Function Definition

void rewinddir(resource $dir_handle)

Description

Resets the directory stream identified by $dir_handle to the beginning of the directory.

Parameters

dir_handle – The directory handle resource previously obtained via opendir() .

Return Value

Returns the directory handle resource on success; returns FALSE on failure.

Example

The following PHP code demonstrates opening a directory, listing its files, rewinding the handle with rewinddir() , listing the files again, and finally closing the directory.

<?php
$dir = "/images/";
if (is_dir($dir)) {
    $dh = opendir($dir);
    // List files in the images directory
    while (($file = readdir($dh)) !== false) {
        echo "filename:" . $file . "<br>";
    }
    // Reset the directory handle to the beginning
    rewinddir();
    // List files again after rewinding
    while (($file = readdir($dh)) !== false) {
        echo "filename:" . $file . "<br>";
    }
    closedir($dh);
}
?>
BackendPHPFilesystemrewinddirdirectory
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.