Friday, October 17, 2025
HomeLanguagesPHP | DirectoryIterator isDir() Function

PHP | DirectoryIterator isDir() Function

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.

Reference: https://www.php.net/manual/en/directoryiterator.isdir.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS