Open In App

PHP | DirectoryIterator getType() Function

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The DirectoryIterator::getType() function is an inbuilt function in PHP which is used to check the type of the current DirectoryIterator item.

Syntax:

string DirectoryIterator::getType( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns a string which represents the type of the file. The type may be one of the file, link, or dir.

Below programs illustrate the DirectoryIterator::getType() 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 it is directory or not
    if ($directory->isDir()) {
        $file = $directory->current();
        echo $file->getFilename() . " | Type: "
                . $directory->getType() . "<br>";
    }
  
    // Move to the next element of DirectoryIterator
    $directory->next();
}
  
?>


Output:

. | Type: dir
.. | Type: dir
dashboard | Type: dir
img | Type: dir
webalizer | Type: dir
xampp | Type: dir

Program 2:




<?php
  
// Create a directory Iterator
$directory = new DirectoryIterator(dirname(__FILE__));
  
// Loop runs for each element of directory
foreach($directory as $dir) {
      
    $file = $directory->current();
      
    echo $dir->key() . " => "
        $file->getFilename() . " | Type: " .
        $dir->getType() . "<br>";
}
  
?>


Output:

0 => . | Type: dir
1 => .. | Type: dir
2 => applications.html | Type: file
3 => bitnami.css | Type: file
4 => dashboard | Type: dir
5 => favicon.ico | Type: file
6 => geeks.PNG | Type: file
7 => gfg.php | Type: file
8 => img | Type: dir
9 => index.php | Type: file
10 => webalizer | Type: dir
11 => xampp | Type: dir

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads