PHP | DirectoryIterator current() Function

The DirectoryIterator::current() function is an inbuilt function in PHP which is used to return the current DirectoryIterator item.

Syntax:

DirectoryIterator DirectoryIterator::current( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the current DirectoryIterator item.

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



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
  
    // Check for directory element
    if ($directory->isDir()) {
  
        // Store the current element
        $file = $directory->current();
  
        // Display the filename
        echo $file->getFilename() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
?> 

chevron_right


Output:

.
..
dashboard
img
webalizer
xampp

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    // Store the current element
    $file = $directory->current();
      
    // Display the key and filename
    echo $dir->key() . " => "
        $file->getFilename() . "<br>";
}
?>

chevron_right


Output:

0 => .
1 => ..
2 => applications.html
3 => bitnami.css
4 => dashboard
5 => favicon.ico
6 => geeks.PNG
7 => gfg.php
8 => img
9 => index.php
10 => Sublime Text Build 3211 x64 Setup.exe
11 => webalizer
12 => xampp

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

Reference: https://www.php.net/manual/en/directoryiterator.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.