Monday, December 22, 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
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS