Cross-Platform, PHP

Get File extension in PHP

Let’s see three different ways to get file extension. Just pick one!

1
2
3
4
5
6
7
8
9
$filename = 'myfile.txt';
$ext = substr(strrchr($filename, '.'), 1);
echo $ext."<br>";
 
$ext = substr($filename, strrpos($filename, '.') + 1);
echo $ext."<br>";
 
$ext = preg_replace('/^.*.([^.]+)$/D', '$1', $filename);
echo $ext."<br>";