PHP | DirectoryIterator getType() Function
Last Updated :
07 Mar, 2024
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
$directory = new DirectoryIterator(dirname( __FILE__ ));
while ( $directory ->valid()) {
if ( $directory ->isDir()) {
$file = $directory ->current();
echo $file ->getFilename() . " | Type: "
. $directory -> getType () . "<br>" ;
}
$directory ->next();
}
?>
|
Output:
. | Type: dir
.. | Type: dir
dashboard | Type: dir
img | Type: dir
webalizer | Type: dir
xampp | Type: dir
Program 2:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
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
Please Login to comment...