Open In App

PHP | FilesystemIterator rewind() Function

Last Updated : 26 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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
geeks.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\geeks.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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads