Tuesday, June 9, 2026
HomeLanguagesPHP | DirectoryIterator isFile() Function

PHP | DirectoryIterator isFile() Function

The DirectoryIterator::isFile() function is an inbuilt function in PHP which is used to check the current DirectoryIterator item is a regular file or not.

Syntax:

bool DirectoryIterator::isFile( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns TRUE if the file exists and is a regular file (not a link or dir), otherwise FALSE.

Below programs illustrate the DirectoryIterator::isFile() 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) {
      
    // Check the directory element is file
    if($dir->isFile()) {
  
        // Display the filename
        echo $dir->getFilename() . "<br>";
    }
}
  
?>


Output:

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

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
      
    // Check the directory element is file
    if($directory->isFile()) {
  
        // 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.isfile.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS