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.

<?php
$extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
?>

Examples:
DirectoryIterator::getExtension() example
<?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
<?php
$extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
?>

See also:

DirectoryIterator::getFilename() -

DirectoryIterator::getBasename() -

pathinfo() -

doc_php
2016-02-24 16:19:46
Comments
Leave a Comment

Please login to continue.