HomeLanguagesPHP | DirectoryIterator getType() Function

PHP | DirectoryIterator getType() Function

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 => neveropen.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.

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

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32736 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7116 POSTS0 COMMENTS
Nicole Veronica
12250 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12333 POSTS0 COMMENTS
Shaida Kate Naidoo
7228 POSTS0 COMMENTS
Ted Musemwa
7488 POSTS0 COMMENTS
Thapelo Manthata
7208 POSTS0 COMMENTS
Umr Jansen
7180 POSTS0 COMMENTS