PHP check file extension

PHP check file extension

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

Stefan Bogdanescu

Founder & Senior Architect

Robust File Extension Validation in PHP

Handling file uploads requires moving beyond simple string matching against $_FILES['name']. The initial approach shown in your snippet—checking for a specific extension and calling a corresponding function—is logically sound, but it must be fortified against client-side manipulation. Browsers and users can easily rename files to mimic allowed extensions like .jpg or .php, so relying solely on the extension provided by the client is insufficient for security.

The first step in any robust implementation is extracting the real file extension from the uploaded stream itself. In PHP, you access the temporary filename via $_FILES['upload']['tmp_name']. You can then use finfo_open() with MIME type detection to verify what the server actually sees.

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['upload']['tmp_name']);
fclose($finfo);

// Whitelist specific types: image/jpeg, image/png, application/pdf
if (in_array($mime, ['image/jpeg', 'image/png', 'application/pdf'])) {
    // Proceed with upload logic
}

This MIME check is a necessary baseline, but it still leaves you vulnerable to MIME sniffing attacks where malicious files claim to be images while containing PHP code. To truly secure the extension check, you must correlate the file extension with the detected MIME type using a strict whitelist. Do not allow image/jpeg if the file extension is .jpg, unless you have explicitly whitelisted that combination.

Once you have validated the file, you can proceed to your branching logic based on the extension. Here is how to structure the conditional block safely:

$allowedExtensions = [
    'jpg' => ['function_handle_image', 'image/jpeg'],
    'png' => ['function_handle_image', 'image/png'],
    'pdf' => ['function_handle_document', 'application/pdf']
];

$file_extension = strtolower(pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION));

if (isset($allowedExtensions[$file_extension])) {
    $handler_function = $allowedExtensions[$file_extension][0];
    $allowed_mime_type = $allowedExtensions[$file_extension][1];

    // Re-verify MIME type matches the allowed one for this extension
    if ($mime === $allowed_mime_type) {
        $function_handle_image($uploaded_file);
    } else {
        throw new InvalidArgumentException('MIME type mismatch detected.');
    }
} else {
    throw new InvalidArgumentException('File type not supported.');
}

This pattern ensures that FUNCTION1 only runs if the file extension is valid, the MIME type matches, and the MIME type itself was pre-approved. A common pitfall is dynamically evaluating user input or using preg_match to extract extensions without strict bounds. Always sanitize the input by lowercasing it and trimming whitespace before comparison.

If you are building a large-scale application where this logic repeats across many endpoints, consider refactoring the validation into a service class or using a dependency injection container. For Laravel projects specifically, you might leverage existing packages or the built-in File facade to handle these checks more declaratively. While custom solutions offer granular control over extension-to-function mapping, frameworks like https://laravelcompany.com provide robust abstractions that can simplify file handling if your architecture demands it.

Remember that validation happens on the server, never just in the HTML form or JavaScript. Even if a user cannot upload a PHP file due to server configuration (php.ini), you must still check extensions for other dangerous types like .phtml, .phar, or .htaccess. Always maintain an immutable list of allowed extensions and MIME types that are reviewed periodically against emerging threats.

Our network