Finding Extension of uploaded file (PHP)

Finding Extension of uploaded file (PHP)

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

Stefan Bogdanescu

Founder & Senior Architect

Finding Extension of Uploaded File (PHP)

When handling file uploads in PHP, a common requirement is verifying that the uploaded content matches the expected file type before processing or storing it. Developers often assume that the extension provided by the client is trustworthy, but relying on $_FILES['file']['name'] alone is insufficient for security-critical applications. The core question arises: can we determine the original file's extension directly from the temporary path stored in $_FILES['file']['tmp_name']?

The short answer is no. PHP does not expose the original filename or its extension via the temporary file path. When a file is uploaded, PHP moves it to a system-defined temporary directory (often /tmp on Linux or the Windows equivalent) and renames it using a unique identifier generated by uniqid(). This identifier typically consists of a timestamp and random characters, stripping away any user-provided metadata, including the original extension. For example, if a user uploads image.jpg, PHP might save it as /var/tmp/php_658e3a2f.tmp. There is no API call or function that retrieves the original filename from this temporary path without re-uploading the file or accessing server logs that record the initial request data.

To verify the actual file type, you must inspect the file's content rather than its name. The most robust approach is using the finfo extension in PHP, which leverages the underlying libmagic library to analyze file signatures (also known as "magic numbers"). This method reads the first few bytes of the file to determine its true MIME type, regardless of what the client claims it is.

Here is a practical implementation using finfo:

$uploadDir = '/uploads/';
$tempFile = $_FILES['file']['tmp_name'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$realMimeType = $finfo->file($tempFile);

// Validate against allowed types
$allowedMimes = ['image/jpeg', 'image/png', 'image/gif'];

if (in_array($realMimeType, $allowedMimes)) {
    // File is safe to process
} else {
    // Reject the file
}

Alternatively, you can use exif_imagetype() for specific image formats. This function reads the image header and returns a constant representing the image type (e.g., IMAGETYPE_JPEG). However, this method only works reliably for standard images and may fail with corrupted files or non-standard headers. It also does not work for PDFs, Office documents, or other binary formats where finfo is superior.

A critical pitfall to avoid is trusting the $_FILES['file']['type'] field. While this value comes from the client's HTTP header (Content-Type), it is easily spoofable. A malicious actor can rename an executable .exe file to image.jpg and set the content type to image/jpeg. Without inspecting the actual binary data, your application could inadvertently execute arbitrary code or serve malware.

Another consideration is handling files that claim to have no extension or use double extensions (e.g., file.jpg.exe). Relying on the original name for sanitization paths can lead to vulnerabilities if not properly escaped. Always generate a new filename based on the verified MIME type and a random string when saving the file permanently.

For Laravel developers, this logic integrates seamlessly into custom upload handlers or middleware. You might wrap the finfo check in a utility service that returns a validated object containing the real MIME type and the original name if needed for logging. Remember to configure your web server (Nginx or Apache) to prevent directory listing and ensure that uploaded files are stored outside the public root to avoid direct access risks.

By shifting trust from filenames to file signatures, you build a more resilient upload pipeline that withstands common spoofing attempts.

Our network