Open In App

PHP | DirectoryIterator isDir() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

bool DirectoryIterator::isDir( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns TRUE if the directory exist, FALSE otherwise.

Below programs illustrate the DirectoryIterator::isDir() function in PHP:

Program 1:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
  
    // Check for directory element
    if ($directory->isDir()) {
        $file = $directory->current();
  
        // Display the filename and its size
        echo $file->getFilename() . " | Size: "
                . $directory->getSize() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
  
?>


Output:

. | Size: 4096
.. | Size: 12288
dashboard | Size: 4096
img | Size: 0
webalizer | Size: 0
xampp | Size: 0

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs while directory is valid
while ($directory->valid()) {
  
    // Check for directory element
    if ($directory->isDir()) {
        $file = $directory->current();
  
        // Display file name and last modified time
        echo $file->getFilename() . " | MTime: "
                . $directory->getMTime() . "<br>";
    }
  
    // Move to the next element
    $directory->next();
}
?> 


Output:

. | MTime: 1574654324
.. | MTime: 1574540515
dashboard | MTime: 1574350724
img | MTime: 1574350724
webalizer | MTime: 1574350718
xampp | MTime: 1574350724

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