How to Get a File Extension in PHP: Best Practices 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Get a File Extension in PHP: From Beginner to Pro in 2025

\n\n

Have you ever wondered how many times a day a PHP developer has to extract a file extension? Dozens, if not hundreds. Uploading avatars, processing documents, validating images — everywhere you need to understand what kind of creature has arrived: .jpg, .pdf, or a tricky .php.jpg.

\n\n

The question "How can I get a file's extension in PHP?" is one of the most popular on Stack Overflow. And for good reason. It seems like it couldn't be simpler? But the devil, as always, is in the details. In 2025, we have not only the old faithful pathinfo() and explode(), but also new capabilities in PHP 8.4 that make code cleaner and safer.

\n\n

Let's break down 5 working methods, their pros and cons, and when to use each. Let's go!

\n\n

1. The Classic: pathinfo() — The King of All Methods

\n\n

The most popular and correct way is the pathinfo() function. It appeared back in PHP 4 and is still relevant today. Why? Because it's smart: it accounts for system path separators, works with URLs, and doesn't break on dots in the filename.

\n\n
<?php\n$file = 'document.report.pdf';\n$extension = pathinfo($file, PATHINFO_EXTENSION);\n// Result: 'pdf'\n?>
\n\n

But there's a nuance. If the file has no extension, pathinfo() returns an empty string. Not null, not false — but exactly an empty string. This is important for strict checks.

\n\n
<?php\n$file = 'README';\n$ext = pathinfo($file, PATHINFO_EXTENSION);\nvar_dump($ext); // string(0) ""\n?>
\n\n

When to use: whenever you need to quickly and reliably get the extension. It's the industry standard.

\n\n

2. The Old School Method: explode() and strrpos()

\n\n

Some old-school developers prefer manual string parsing. The idea is simple: find the last dot and take everything after it.

\n\n
<?php\n$file = 'image.jpg';\n$parts = explode('.', $file);\n$extension = end($parts);\n// Result: 'jpg'\n?>
\n\n

It seems to work. But there's a catch: explode() creates an array, and end() moves the internal array pointer. Not critical, but inelegant.

\n\n

A more performant option is strrpos() + substr():

\n\n
<?php\n$file = 'archive.tar.gz';\n$pos = strrpos($file, '.');\n$extension = $pos !== false ? substr($file, $pos + 1) : '';\n// Result: 'gz'\n?>
\n\n

Note: strrpos() finds the last occurrence of the dot. This is correct — we want exactly gz, not tar.gz.

\n\n

When to use: when pathinfo() is unavailable (exotic cases) or you need more control over the logic.

\n\n

3. New in 2025: str_contains() and str_starts_with()

\n\n

PHP 8.0 introduced str_contains() and str_starts_with(), and PHP 8.4 brought improved multibyte string handling. This opens up new elegant approaches.

\n\n
<?php\n$file = 'photo.2025.jpg';\n\nif (str_contains($file, '.')) {\n    $extension = substr($file, strrpos($file, '.') + 1);\n} else {\n    $extension = '';\n}\n// Result: 'jpg'\n?>
\n\n

Elegant? Not really. But if you need to check whether a file is an image, str_ends_with() is your friend:

\n\n
<?php\n$file = 'avatar.png';\n$isImage = str_ends_with($file, '.png') || str_ends_with($file, '.jpg');\n// true\n?>
\n\n

When to use: for quick checks of specific extensions without extracting the entire extension.

\n\n

4. Regular Expressions: When You Need Everything at Once

\n\n

Sometimes one extension is not enough. For example, you need to check that a file is a safe image and also extract the name without the extension. This is where

Blogs

Book Recommendations