PHP | FilesystemIterator current() Function

The FilesystemIterator::current() function is an inbuilt function in PHP which is used to returns the file information of the current element.

Syntax:

mixed FilesystemIterator::current( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the filename, file information, or $this depending on the set flags.

Below programs illustrate the FilesystemIterator::current() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>";
}
  
?>

chevron_right


Output:

C:\xampp\htdocs\applications.html
C:\xampp\htdocs\bitnami.css
C:\xampp\htdocs\dashboard
C:\xampp\htdocs\favicon.ico
C:\xampp\htdocs\geeks.PNG
C:\xampp\htdocs\gfg.php
C:\xampp\htdocs\img
C:\xampp\htdocs\index.php
C:\xampp\htdocs\Sublime Text Build 3211 x64 Setup.exe
C:\xampp\htdocs\webalizer
C:\xampp\htdocs\xampp

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new file system iterator
$fileItr = new FilesystemIterator(__DIR__, 
    FilesystemIterator::CURRENT_AS_PATHNAME);
  
// Loop runs file iterator is valid
while ($fileItr->valid()) {
  
    // Check for directory file iterator
    if ($fileItr->isDir()) {
  
        // Display the current element
        echo $fileItr->current() . "<br>";
    }
  
    // Move to the next file iterator
    $fileItr->next();
}
  
?>

chevron_right


Output:

C:\xampp\htdocs\dashboard
C:\xampp\htdocs\img
C:\xampp\htdocs\webalizer
C:\xampp\htdocs\xampp

Note: The output of this function depends on the content of server folder.

Reference: https://www.php.net/manual/en/filesystemiterator.current.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.