PHP Tutorial

PHP Examples

PHP Practice

PHP Directory rewinddir() Function (With Examples)

In this tutorial, we will learn about the PHP rewinddir() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP rewinddir() function

The rewinddir() function is used to rewind/reset the directory handle which is created by the opendir() function.

Syntax

The syntax of the rewinddir() function:

rewinddir(dir_handle);

Parameters

The parameters of the rewinddir() function:

  • dir_handle – it is an optional parameter which is used to specify the directory handle resource if we do not specify the directory handle resource – it assumes the last link opened with opendir() function.

Return Value

It returns nothing.

PHP rewinddir() Function Example

PHP code to demonstrate example of rewinddir() function.

<?php

$path = "/home";

//checking whether $path is a directory or not
//then, opening the directory and reading its files
if (is_dir($path)) {
    if ($dh = opendir($path)) {
        while (($file = readdir($dh)) !== false) {
            echo "File:" . $file . "<br/>";
        }

        //rewinding/reset the directory handle
        rewinddir();

        //reading the files again
        while (($file = readdir($dh)) !== false) {
            echo "File:" . $file . "<br/>";
        }

        //closing the directory
        closedir($dh);
    }
}
?>

Output

The output of the above example is:

File:.
File:..
File:main.php
File:.
File:..
File:main.php

Reference: PHP rewinddir() function

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.