PHP | DirectoryIterator getExtension() Function
Last Updated :
26 Nov, 2019
The DirectoryIterator::getExtension() function is an inbuilt function in PHP which is used to get the file extension.
Syntax:
string DirectoryIterator::getExtension( void )
Parameters: This function does not accept any parameters.
Return Value: This function returns a string which contain the file extension, or an empty string if the file does not contain extension.
Below programs illustrate the DirectoryIterator::getExtension() function in PHP:
Program 1:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
foreach ( $directory as $dir ) {
echo $dir ->getExtension() . "<br>" ;
}
?>
|
Output:
html
css
ico
PNG
php
php
Note: Empty lines denote the folder which does not contain file extension.
Program 2:
<?php
$directory = new DirectoryIterator(dirname( __FILE__ ));
while ( $directory ->valid()) {
if (! $directory ->isDir()) {
echo $directory ->getExtension() . "<br>" ;
}
$directory ->next();
}
?>
|
Output:
html
css
ico
PNG
php
php
Note: The output of this function depends on the content of server folder.
Reference: https://www.php.net/manual/en/directoryiterator.getextension.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...