Syntax:void FilesystemIterator::rewind (void)
Parameters:This function does not accept any parameters.
Returned value:This function does not return any value.The following programs illustrate the FilesystemIterator function::rewind() in PHP:
Program 1:
// Create a new file system iterator
$fileItr
=
new
FilesystemIterator (dirname (
__ FILE__
),
FilesystemIterator::KEY_AS_FILENAME);
// Loop starts while file iterator is active
while
(
$fileItr
-> valid()) {
// Validate uncatalogued item
if
(!
$fileItr
-> isDir() ) {
// Show key
echo
$fileItr
-> key().
"
"
;
}
// Move to next item
$fileItr
-> next();
}
// Rewind to starting position
$fileItr
->
rewind
();
// Show key
echo
"
After using rewind() function
"
;
echo
$fileItr
-> key();
?>
Output: applications.html bitnami.css favicon.ico engineer.html gfg.php index.php After using rewind() function applications.html
Program 2:
// Create a new file system iterator
$fileItr
=
new
FilesystemIterator (__ DIR__,
FilesystemIterator::CURRENT_AS_PATHNAME);
// loop is performed for each element
foreach
(
$fileItr
as
$it
) {
// Show current element
echo
$fileItr
-> current().
"
"
;
}
// Rewind to starting position
$fileItr
->
rewind
();
// Show key
echo
"
After using rewind() function
"
;
echo
$fileItr
-> key();
?>
Output: C: xampphtdocsapplications.html C: xampphtdocsitnami.css C: xampphtdocsdashboard C: xampphtdocsfavicon.ico C: xampphtdocsengineer.html C: xampphtdocsgfg.php C: xampphtdocstd C: xampphtdhtd ) function C: xampphtdocsapplications.html
Note . The output of this function depends on the contents of the server folder.Link: https : //www.php.net/manual/en/filesystemiterator.rewind.php