Open In App

PHP | DirectoryIterator isFile() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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
geeks.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
geeks.PNG
gfg.php
index.php

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



Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads