Saturday, December 28, 2024
Google search engine
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

Recent Comments