In this code example we will understand how to get file extension in PHP this is very useful when you extract part of the file name like filename, file extension maybe if you want to change the extension of an existing file.


Here is the original file path we will extract the file extension and filename without file extension

https://rathorji.in/post-feature_thumb/3de3d0cde46156532187244eb383ec72/5554e65275c51f06e1d9055f54a44b96.jpg


#example.php

<?php

$url = 'https://rathorji.in/post-feature_thumb/3de3d0cde46156532187244eb383ec72/5554e65275c51f06e1d9055f54a44b96.jpg';
$file = file_get_contents($url); // To get file
$name = basename($url); // To get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // To get extension
$name2 = pathinfo($url, PATHINFO_FILENAME); //  File name without extension

echo $name. "<br>";
echo $ext. "<br> file extension";
echo $name2. " File name without extension <br>";
?>

#output:

5554e65275c51f06e1d9055f54a44b96.jpg
jpg
file extension5554e65275c51f06e1d9055f54a44b96 File name without extension