How to get the file extension using PHP

Question:

How can I grab the extension of a file I have uploaded using PHP?

Answer:

PHP comes with a built-in function called ‘pathinfo()’ which allows you to get the extension of file you have uploaded.

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
echo $ext;

Leave a Comment.