PHP | FilesystemIterator rewind() Function

The FilesystemIterator::rewind() function is an inbuilt function in PHP which is used to rewinds back to the beginning of the file.
Syntax:
void FilesystemIterator::rewind( void )
Parameters: This function does not accept any parameters.
Return Value: This function does not return any value.
Below programs illustrate the FilesystemIterator::rewind() function in PHP:
Program 1:
<?php   // Create new file system iterator $fileItr = new FilesystemIterator(dirname(__FILE__),         FilesystemIterator::KEY_AS_FILENAME);   // Loop runs while file iterator is valid while ($fileItr->valid()) {       // Check for non-directory element     if (!$fileItr->isDir()) {           // Display the key         echo $fileItr->key() . "<br>";     }       // Move to the next element     $fileItr->next(); }   // Rewind back to start position $fileItr->rewind();   // Display the key echo "<br>After using rewind() function <br>";   echo $fileItr->key();   ?> |
Output:
applications.html bitnami.css favicon.ico zambiatek.html gfg.php index.php After using rewind() function applications.html
Program 2:
<?php   // Create new file system iterator $fileItr = new FilesystemIterator(__DIR__,     FilesystemIterator::CURRENT_AS_PATHNAME);   // Loop runs for each element foreach($fileItr as $it) {            // Display the current element     echo $fileItr->current() . "<br>"; }   // Rewind back to start position $fileItr->rewind();   // Display the key echo "<br>After using rewind() function <br>";   echo $fileItr->key();   ?> |
Output:
C:\xampp\htdocs\applications.html C:\xampp\htdocs\bitnami.css C:\xampp\htdocs\dashboard C:\xampp\htdocs\favicon.ico C:\xampp\htdocs\zambiatek.html C:\xampp\htdocs\gfg.php C:\xampp\htdocs\img C:\xampp\htdocs\index.php C:\xampp\htdocs\webalizer C:\xampp\htdocs\xampp After using rewind() function C:\xampp\htdocs\applications.html
Note: The output of this function depends on the content of server folder.
Reference: https://www.php.net/manual/en/filesystemiterator.rewind.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



