How to get the file extension in PHP?

How to get the file extension in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get the file extension in PHP?

When handling file uploads, extracting the file extension is a common requirement for validation and categorization. The source material shows an attempt using explode() on $_FILES['image']['name']. While this works conceptually, it relies on fragile string parsing that can break easily with edge cases like missing dots or multiple extensions (e.g., .tar.gz).

A more robust approach leverages PHP's built-in file extension handling functions. Since PHP 8.0, pathinfo() has been available in a format that explicitly separates the extension, but even before that, manual parsing was often necessary. The key is to isolate the part of the filename after the last dot and ensure it isn't empty or just a dot itself.

Consider the scenario where a user uploads image.png. Using explode('.', strtolower($filename)) produces an array like ['image', 'png']. You would then take the last element. However, if the filename is archive.tar.gz, a simple split at the first dot yields ['archive', 'tar', 'gz'], and taking the last element gives gz, which might not be what you want depending on your logic. More critically, if the input is just .hidden, explode returns ['', 'hidden']. If the input has no dot, explode returns a single-element array containing the whole string.

Here is a safer implementation pattern that handles these variations without relying on brittle index assumptions:

$filename = $_FILES['image']['name'];
$name_without_path = pathinfo($filename, PATHINFO_FILENAME);
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

if ($extension === '') {
    // Handle case where no extension exists or filename is just a dot
    $extension = null; 
}

Using pathinfo() with the PATHINFO_EXTENSION constant is generally preferred because it intelligently handles Windows-style path separators and ensures you are only looking at the file name portion, not directory paths passed in the upload field. The lowercase() conversion is critical for consistent validation logic, as operating systems often treat extensions case-insensitively.

However, there is a specific pitfall to watch out for: hidden files. If a user uploads .htaccess, pathinfo() might return an empty string for the extension depending on how the path is parsed in different PHP versions or environments. Always validate that the resulting extension matches a whitelist of allowed types before processing the file.

For example, if you are building a secure image gallery, you should not rely solely on the client-provided name. Even after extracting the extension via pathinfo(), you must verify that the MIME type matches the claimed extension to prevent spoofing attacks where a user renames an executable as .jpg.

If you find yourself needing to handle complex upload workflows repeatedly across multiple projects, consider using a framework like Laravel. The LaravelCompany resource provides additional context on building robust file handling systems within the ecosystem, though the core logic for extension extraction remains consistent across vanilla PHP and frameworks alike. In Laravel specifically, you might use Request::file('image')->getClientOriginalExtension(), which abstracts much of this parsing away entirely, but understanding the underlying mechanics helps when debugging issues or working with raw input streams.

The trade-off between using pathinfo() and manual string manipulation is minimal in modern PHP versions, but the clarity gained from using standard library functions usually outweighs the slight performance benefit of explode(). Just remember to always sanitize the output before comparing it against your allowed list.

Our network