PHP Directory rewinddir() Function



The PHP directory rewinddir() function resets the position of the last opened directory handle to the beginning, allowing you to read the directory contents again. This function is useful because after using readdir() to read directory contents, you can not read the directory again without resetting it using rewinddir(). And also we can use rewinddir() multiple times.

In simple terms we can say that it resets the directory stream given to by dir_handle to the beginning of the directory.

Syntax

Below is the syntax of the PHP directory rewinddir() function −

void rewinddir ( resource $dir_handle );

Parameters

Here are the required parameter of the rewinddir() function −

Sr.No Parameter & Description
1

dir_handle(Required)

The directory handle resource previously opened with opendir().

Return Value

It returns NULL means not value is returned by this function.

PHP Version

Introduced in core PHP 4, the rewinddir() function works well with PHP 5, PHP 7, and PHP 8.

Example

In this PHP code we will open the directory and read the first entry, then rewind using the PHP directory rewinddir() function and read the first entry again, finally close the directory.

<?php
   // try to open the directory
   $dir = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac");

   // read the first entry
   readdir($dir); 

   // rewind to the beginning
   rewinddir($dir); 

   // read the first entry again
   echo "Read the first entry: ".readdir($dir); 

   // close the directory
   closedir($dir); 
?> 

Output

This will produce the following result −

Read the first entry: .

Example

Now we will use rewinddir() function to show all the content present in the current working directory or the path we have given and we use while loop to traverse the content present in it.

<?php
   // Try to open the directory
   $dir = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac");

   echo "Items in my directory are- " . "<br>";
   while(false !== ($entry = readdir($dir))){
      echo "Item: " . $entry . "<br>";
   }
   rewinddir();
   echo "After rewinddir() function, items in my directory are- " . "<br>";
   while(false !== ($entry = readdir($dir))){
      echo "Item: " . $entry . "<br>";
   }
?> 

Output

This code will lead to the following outcome −

Items in my directory are-
Item: .
Item: ..
Item: logo.gif
Item: .DS_Store
Item: index.php
Item: new dir
Item: images
Item: image.gif
Item: myfile.txt
Item: my.php
After rewinddir() function, items in my directory are-
Item: .
Item: ..
Item: logo.gif
Item: .DS_Store
Item: index.php
Item: new dir
Item: images
Item: image.gif
Item: myfile.txt
Item: my.php

Example

In the below PHP code, we will open the directory, list its contents using readdir(), create a 'Test' directory using mkdir() function, rewind using rewinddir() function, and list the contents again.

<?php
   // Try to open the directory
   $directory = opendir("/Applications/XAMPP/xamppfiles/htdocs");

   echo "Contents of 'htdocs' directory are- "."<br>";
   while(false !== ($entry = readdir($directory))){
      echo $entry . "<br>";
   }
   mkdir("/Applications/XAMPP/xamppfiles/htdocs/Test");
   rewinddir($directory);
   echo "After rewinddir() function, Contents of 'htdocs' directory are- "."<br>";
   while(false !== ($entry = readdir($directory))){
      echo $entry . "<br>";
   }
?> 

Output

This code will lead to the below result −

Contents of 'htdocs' directory are-
.
..
favicon.ico
.DS_Store
index.php
applications.html
webalizer
img
dashboard
mac
bitnami.css
After rewinddir() function, Contents of 'htdocs' directory are-
.
..
favicon.ico
.DS_Store
index.php
Test
applications.html
webalizer
img
dashboard
mac
bitnami.css

Example

Now we will open (using opendir()) and list the contents of two directories ('htdocs' and 'mac') using readdir(), rewind the 'mac' directory using rewinddir(), and then list both directories' contents again.

<?php
   // open the first directory
   $directory1 = opendir("/Applications/XAMPP/xamppfiles/htdocs");

   // open the second directory
   $directory2 = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac");

   echo "Contents of 'htdocs' directory are- ";

   while(false !== ($entry = readdir($directory1))){
      echo $entry . "<br>";
   }

   echo "Contents of 'mac' directory are- ";

   while(false !== ($entry = readdir($directory2))){
      echo $entry . "<br>";
   }

   // rewind the second directory using rewinddir()
   rewinddir($directory2);

   echo "After rewinddir() function, Contents of 'mac' directory are- ";
   while(false !== ($entry = readdir($directory2))){
      echo $entry . "<br>";
   }

   echo "After rewinddir() function, Contents of 'htdocs' directory are- ";
   while(false !== ($entry = readdir($directory1))){
      echo $entry . "<br>";
   }
?> 

Output

The result of this PHP code is −

Contents of 'htdocs' directory are- .
..
favicon.ico
.DS_Store
index.php
applications.html
webalizer
img
dashboard
mac
Contents of 'mac' directory are- .
..
logo.gif
.DS_Store
index.php
images
image.gif
myfile.txt
After rewinddir() function, Contents of 'mac' directory are- .
..
logo.gif
.DS_Store
index.php
images
image.gif
myfile.txt
After rewinddir() function, Contents of 'htdocs' directory are-

Advantage

The advantage of using the rewinddir() method compared to the opendir() function to read directory content is that opendir() uses a new resource, but rewinddir() uses a previous resource. So, rewinddir() optimizes resource utilization.

Note

If a directory handle is not provided, rewinddir() reopens the previous directory that was opened. The error message "Undefined variable" will be displayed if you use the wrong handle.

Summary

The rewinddir() function is a PHP built-in directory function. It in general helps read the content of a directory again and again. It is a very simple function to use.

php_function_reference.htm
Advertisements