Open In App

PHP | DirectoryIterator getFilename() Function

Last Updated : 26 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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
geeks.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
geeks.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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads