PHP | rewinddir() Function

The rewinddir() function is an inbuilt function in PHP which is used to rewind the directory handle. The rewinddir() function opens a directory and list its files, resets the directory handle, list its files once again, then finally closes the directory handle.
The directory handle sent as a parameter to the rewinddir() function and it returns Null on success or False on failure.

Syntax:

rewinddir ( $dir_handle )

Parameters: The rewinddir() function accepts single parameter $dir_handle. It is a mandatory parameter which specifies the handle resource previously opened by the opendir() function.



Return Value: It returns Null on success or False on failure.

Errors And Exceptions:

  • If the directory handle parameter is not specified by the user then the last link opened by opendir() is assumed by the rewinddir() function.
  • rewinddir() is equivalent to a closedir(), opendir() sequence, but without obtaining a new handle.

Below programs illustrate the rewinddir() function in PHP:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Open a directory
$dir_handle = opendir("C:/xampp/htdocs/gfg");
  
// Read the contents of directory
while(($file_name = readdir($dir_handle)) !== false) 
    echo("File Name: " . $file_name . "<br>");
}
  
// Rewinding directory
rewinddir($dir_handle);
  
while(($file_Name = readdir($dir_handle)) !== false) 
    echo("File Name: " . $file_Name . "<br>");
  
// Close directory
closedir($dir_handle);
?>

chevron_right


Output:

File Name: .
File Name: ..
File Name: content.xlsx
File Name: gfg.pdf
File Name: image.jpeg
File Name: .
File Name: ..
File Name: content.xlsx
File Name: gfg.pdf
File Name: image.jpeg

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Directory path
$dir_name = "C:/xampp/htdocs/gfg";
   
// Open directory and read the content
// of directory
if (is_dir($dir_name)) {
  if ($dir_handle = opendir($dir_name)) {
  
    // List files in images directory
    while (($file_name = readdir($dir_handle)) !== false) {
      echo "File Name:" . $file_name . "<br>";
    }
  
    // Rewing the directory
    rewinddir();
  
    // List once again files in images directory
    while (($file_name = readdir($dir_handle)) !== false) {
      echo "File Name:" . $file_name . "<br>";
    }
  
    // Close the directory
    closedir($dir_handle);
  }
}
?>

chevron_right


Output:

filename:.
filename:..
filename:content.xlsx
filename:gfg.pdf
filename:image.jpeg
filename:.
filename:..
filename:content.xlsx
filename:gfg.pdf
filename:image.jpeg

Reference: http://php.net/manual/en/function.rewinddir.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.