PHP rewinddir() Function

What is PHP rewinddir() Function?

The rewinddir() function facilitates you to reset the last opened directory handle to the beginning of the directory so that you can read the directory content again. You know after reading a directory content with readdir() function, you can’t re-read directory again. With the rewinddir() function, you can do this as many time as you want. “rewinddir” is short for “Rewind directory”.

The advantage of using rewinddir() function over using opendir() function again to read directory content is opendir() function use a new resource while rewinddir() use the previous the same resource. So, rewinddir() optimizes resource usage.

Syntax:

rewinddir(dir_handle)

Parameters:

The Function has 1 parameter which is optional-

dir_handle (Optional): It is a directory handle that was opened previously by the opendir() function. If you don’t mention the parameter, it uses the last opened directory handle by opendir() function. Check example 1.

Return Values:

The function returns-

  • NULL – if it can reset the directory handle to the beginning.

How rewinddir() function works?

When you use rewinddir() function, it reads the directory content (ex. files, folders) mentioned directory handle from the beginning again and places the directory handle to the beginning of the directory. This function works like as though you’ve declared the opendir() function again.

Please note that after using this function, don’t expect that you’ll get the same directory content that you get before running this function. Because before declaring this function you might add or remove any file or folder. Check example 2.

Examples:

Example 1: Simple use of rewinddir() function-

<?php
$dir_handle = opendir("./del/");
echo "Files/folders of 'del' directory are- "."<br />";
while(false !== ($entry = readdir($dir_handle))){
    echo "Files/folders are: " . $entry . "<br />";
}
rewinddir();
echo "After rewinddir() function, Files/folders of 'del' directory are- "."<br />";
while(false !== ($entry = readdir($dir_handle))){
    echo "Files/folders are: " . $entry . "<br />";
}
?>

Output:

Files/folders of ‘del’ directory are-
.
..
New folder
After rewinddir() function, Files/folders of ‘del’ directory are-
.
..
New folder

Explanation:

After traversing all the directory content by the readdir() function (line 4-6), the dir_handle handle doesn’t points to any content of the directory. Then, rewinddir() function resets the directory handle to the first item of the directory (line 7). That’s why, when we run the readdir() function again (line 9-11), it can list the directory items.

Example 2: Getting the latest directory content with rewinddir() function-

<?php
$dir_handle = opendir("./del/");
echo "Files/folders of 'del' directory are- "."<br />";
while(false !== ($entry = readdir($dir_handle))){
    echo $entry . "<br />";
}
mkdir("./del/Test");
rewinddir($dir_handle);
echo "After rewinddir() function, Files/folders of 'del' directory are- "."<br />";
while(false !== ($entry = readdir($dir_handle))){
    echo $entry . "<br />";
}
?>

Output:

Files/folders of ‘del’ directory are-
.
..
New folder
After rewinddir() function, Files/folders of ‘del’ directory are-
.
..
New folder
Test

Explanation:

A new folder “Test” is created in “del” directory (line 7) after listing the directory content by the readdir() function (line 4-6), and then PHP executes the rewinddir() function, the next listing (line 10-12) displays this “Test” folder along with the previous listing.

Example 3: Using the rewinder() function when multiple directory handles are open-

<?php
$dir_handle = opendir("./file/");
$dir_handle2 = opendir("./del/");
echo "Files/folders of 'file' directory are- ";
while(false !== ($entry = readdir($dir_handle))){
    echo $entry . "<br />";
}
echo "Files/folders of 'del' directory are- ";
while(false !== ($entry = readdir($dir_handle2))){
    echo $entry . "<br />";
}
rewinddir();
echo "After rewinddir() function, Files/folders of 'del' directory are- ";
while(false !== ($entry = readdir($dir_handle2))){
    echo $entry . "<br />";
}
echo "After rewinddir() function, Files/folders of 'file' directory are- ";
while(false !== ($entry = readdir($dir_handle))){
    echo $entry . "<br />";
}
?>

Output:

Files/folders of ‘file’ directory are-
.
..
abc.jpg
function.docx
office
shorishartel.jpg
test
xyz.jpg
Files/folders of ‘del’ directory are-
.
..
New folder
After rewinddir() function, Files/folders of ‘del’ directory are-
.
..
New folder
After rewinddir() function, Files/folders of ‘file’ directory are-

Explanation:

Line 1-2: There are two directory handles opened – $dir_handle and $dir_handle2.

Line 12: When we run rewinddir() function it only reset the last directory handle ($dir_handle2) not the both handles.

Line 14-16: As rewinddir() function resets $dir_handle handle, readdir() function can read content of the $dir_handle2 handle.

Line 18-20: And readdir() function can’t read content of the $dir_handle handle.

Example 4: rewinddir() function with wrong directory handle-

<?php
$dir_handle = opendir("./del/");
while(false !== ($entry = readdir($dir_handle))){
    echo "Files/folders are: " . $entry . "<br />";
}
rewinddir($wrong_dir_handle);
while(false !== ($entry = readdir($dir_handle))){
    echo "Files/folders are: " . $entry . "<br />";
}
?>

Output:

Files/folders are: .
Files/folders are: ..
Files/folders are: New folder

Warning: Undefined variable $wrong_dir_handle in D:\xampp\htdocs\php\rewinddir.php on line 6
Files/folders are: .
Files/folders are: ..
Files/folders are: New folder

Explanation:

For supplying wrong directory handle ($wrong_dir_handle), the PHP displays “Undefined variable” warning. But, the rwinddir() function successfully reset the $dir_handle directory handle which is the last open directory handle.

Practical Usages of rewinddir() Function:

When you want to read directory content multiple times, use rewinddir() function. In another case you can use this function when you need to display directory content with loops.

Notes on rewinddir() Function:

  • If there are multiple directory opened and then you use rewinddir() function without mentioning a specific directory handle , this function opens the last directory handle. Check example 3.
  • Even though, you supply a wrong directory handle, the rewinddir() function resets last opened directory handle. Though, for the wrong directory handle, you’ll get a “Undefined variable” warning also. Check example 4.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP rewinddir() Function

rewinddir() function is a PHP built-in directory function that help you to re-read a directory content again and again. It is an easy to use and resource efficient function.

Reference:

https://www.php.net/manual/en/function.rewinddir.php