How to Get a File Extension in PHP: Best Practices for 2025
\n\nHave you ever encountered a situation where you need to quickly determine what type of file a user is uploading — an image, a document, or an archive? Or maybe you're writing a file parser and need to dynamically handle hundreds of extensions? The question "How can I get a file's extension in PHP?" has been among the top most popular questions on Stack Overflow for years, racking up hundreds of thousands of views. And this is no coincidence.
\n\nIt would seem, what could be simpler? Take the string, find the dot, cut off the tail. But PHP is a language with character. It offers several built-in functions at once, and choosing the right one depends on the context. In 2025, when security and performance are more important than ever, we will break down all the current methods and identify the best one.
\n\nIn this article, you will learn:
\n- \n
- Five working ways to get a file extension in PHP. \n
- Which method to use in 95% of cases. \n
- How to avoid the trap with files without an extension or with multiple dots. \n
- Real code examples for copying. \n
Method 1: The Classic — pathinfo()
\n\nIf you ask any experienced PHP developer how to get a file extension, they will answer without hesitation: pathinfo(). This is the most popular and recommended method. The function returns an array with path information: directory, filename, extension, and basename.
<?php\n$file = 'document.pdf';\n$extension = pathinfo($file, PATHINFO_EXTENSION);\necho $extension; // Outputs: pdf\n?>\n\n\nThe beauty of this method lies in its simplicity and readability. You pass the filename and the constant PATHINFO_EXTENSION — and you get a clean result without extra characters. pathinfo() correctly handles most standard cases, including files with multiple dots, such as archive.tar.gz — in this case, it will return gz, which is logical as it is the final extension.
Pros: intuitive, built-in function, works with paths of any complexity.
\nCons: may return an empty string if the file has no extension.
\n\nMethod 2: Working with Strings — strrchr() + substr()
\n\nSometimes in legacy code or in tutorial examples, you can find manual parsing. The idea is simple: find the last dot in the string and take everything after it.
\n\n<?php\n$file = 'image.jpg';\n$extension = substr(strrchr($file, '.'), 1);\necho $extension; // Outputs: jpg\n?>\n\n\nThe function strrchr() finds the last occurrence of a character and returns everything starting from it (including the dot). substr() with the parameter 1 cuts off the dot. It works reliably, but the code is less readable.
Interesting fact: this method is a direct descendant of the era of C-like functions. It is faster than pathinfo() in micro-benchmarks, but the difference is negligible in real projects. Use it if you are writing a high-load parser and every microsecond counts.
Method 3: Quick and Dirty — explode()
\n\nThe most naive method that comes to a beginner's mind: split the string by the dot and take the last element of the array.
\n\n<?php\n$file = 'report.xlsx';\n$parts = explode('.', $file);\n$extension = end($parts);\necho $extension; // Outputs: xlsx\n?>\n\n\nCaution: this is bad practice. Why? Imagine a file my.file.name.txt. explode() will split it into 4 parts, and end() will return txt. That seems correct. But what if the file is named .htaccess? The array will be ['', 'htaccess'], and the extension will be returned as htaccess, even though that is the entire filename. Or a file without an extension — the name itself will be returned.
This method is