Wednesday, November 20, 2024
Google search engine
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

Recent Comments