DirectoryIterator::getExtension

(PHP 5 >= 5.3.6, PHP 7)
Gets the file extension
public string DirectoryIterator::getExtension ( void )

Retrieves the file extension.

Returns:

Returns a string containing the file extension, or an empty string if the file has no extension.

Notes:

This method is only available as of PHP 5.3.6. Another way of getting the extension is to use the pathinfo() function.

1
2
3
<?php
$extension pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
?>
Examples:
DirectoryIterator::getExtension() example
1
2
3
4
5
6
7
8
9
10
<?php
 
$directory new DirectoryIterator(__DIR__);
foreach ($directory as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getExtension() . "\n";
    }
}
 
?>

The above example will output something similar to:

php
txt
jpg
gz
1
2
3
<?php
$extension pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
?>
See also:

DirectoryIterator::getFilename() -

DirectoryIterator::getBasename() -

pathinfo() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.