How to Get File Extension From File Path in PHP

03-Apr-2023

.

Admin

Hi guys,

In this example, i will explain how to get file extension in php. it can get file extension in php. we will find file extension in php. Now this examples there are many ways to get file extension in PHP.we will show how to get file name without extension in php.

The file extension is most important for file validation.for example when you upload some file and then you need to file type validation. like image extension jpg, png, and gif and file extension zip, pdf, and doc.

Here example of Get File Extension In Php.

Exmaple 1:


Now in this example, i will use explode php function using get file extension.

<?php

$filename = 'nicesnippets.png';

$ext = explode('.', $filename);

echo $ext[1];

?>

output

png

Exmaple 2:

Here in this example, we will use end And explode php function using get file extension.

<?php

$filename = 'nicesnippets.pdf';

$ext = end(explode(".", $filename));

echo $ext;

?>

output

pdf

Exmaple 3:

Blow in this example, It will use pathinfo php function using get file extension.

<?php

$filename = 'nicesnippets.zip';

$ext = pathinfo($filename, PATHINFO_EXTENSION);

echo $ext;

?>

output

zip

Exmaple 4:

Here in this example, I will use substr and strrpos php function using get file name extension in php.

<?php

$filename = 'nicesnippets.exe';

$ext = substr($filename, strrpos($filename, '.', -1), strlen($filename));

echo $ext;

?>

output

exe

It will help you...

#PHP