Tutorial Example

A Simple Way to Get File Extension in PHP – PHP Tutorial

Getting file extension can allow us to apply different operation by file extension. For example, you should set different http header by image extension. In this tutorial, we will write a simple example to show you how to get file extension easily by using php.

Define a file

<?php
$filename = "image.png";
?>

Get file extension

$extension = pathinfo($filename, PATHINFO_EXTENSION);
echo $extension;

The output is: png

Notice: the file extension may be empty, if a file does not contain an extension.