Friday, October 24, 2025
HomeLanguagesPHP | DirectoryIterator getFilename() Function

PHP | DirectoryIterator getFilename() Function

The DirectoryIterator::getFilename() function is an inbuilt function in PHP which is used to return file name of current DirectoryIterator item.

Syntax:

string DirectoryIterator::getFilename( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the file name of the current DirectoryIterator item.

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

Program 1:




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


Output:

.
..
applications.html
bitnami.css
dashboard
favicon.ico
neveropen.PNG
gfg.php
img
index.php
webalizer
xampp

Program 2:




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


Output:

applications.html
bitnami.css
favicon.ico
neveropen.PNG
gfg.php
index.php

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

Reference: https://www.php.net/manual/en/directoryiterator.getfilename.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS