
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.