PHP

How to Get File Extension in PHP


In this article, you will learn how to get the file extension of a file in PHP.

Let’s say you have a string variable named ‘a’ that has the value of a local file path.

$a = "‪‪C:\Users\Codesource\Desktop\test.txt";

Get File Extension

In order to get the file extension of a file, you can use the pathinfo() method.

$a = "‪‪C:\Users\Codesource\Desktop\test.txt";

$file_extension = pathinfo($a, PATHINFO_EXTENSION);

echo "The file extension is ".$file_extension;
// The file extension is txt

Note: The pathinfo() method functions by returning information about the supplied file path.


Share on social media

//