solvit.io

Get the extension of a file in PHP

Solution
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
Example
$uploadedImage = 'screenshot1234.png';
$fileExtension = pathinfo($uploadedImage, PATHINFO_EXTENSION);
// $fileExtension = 'png'

If the file has two or more extensions (e.g. index.html.twig) this function only returns the last one (e.g. twig).

Find more solutions about: , ,