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:
<?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); ?> |
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:
<?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); } } ?> |
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
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | end() Function
- p5.js | hex() function
- PHP | Ds\Map first() Function
- PHP Ds\Map sum() Function
- D3.js | d3.rgb() Function
- p5.js | int() function
- D3.js | d3.hcl() Function
- CSS | hsl() Function
- PHP | next() Function
- PHP | pow( ) Function
- PHP | dir() Function
- D3.js | d3.lab() Function
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.



