php better way to handle file extension

Solution:

Quoting from the duplicate question’s top answer:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

this is the best available way to go. It’s provided by the operating system, and the best you can do. I know of no cases where it doesn’t work.

One exception would be a file extension that contains a .. But no sane person would introduce a file extension like that, because it would break everywhere plus it would break the implicit convention.

for example in a file 20121021.my.file.name.txt.tar.gz – tar.gz would be the extention..

Nope, it’s much simpler – and maybe that is the root of your worries. The extension of 20121021.my.file.name.txt.tar.gz is .gz. It is a gzipped .gz file for all intents and purposes. Only when you unzip it, it becomes a .tar file. Until then, the .tar in the file name is meaningless and serves only as information for the gunzip tool. There is no file extension named .tar.gz.

That said, detecting the file extension will not help you determine whether a file is actually of the type it claims. But I’m sure you know that, just putting this here for future readers.