How to check uploaded file type in PHP

How to check uploaded file type in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Why Relying on $_FILES['type'] Breaks Your File Validation

When users complain that your image upload logic fails intermittently—working for some browsers but rejecting valid files from others—the root cause is almost certainly how you are reading the file metadata. The snippet provided in the source material attempts to validate an image by checking $_FILES['fupload']['type'] against a string list of MIME types. While this approach looks straightforward, it relies on client-side data that is notoriously unreliable.

Browsers and web clients populate the type field based on what they think the file is, often reading the extension or guessing the content. However, malicious actors can easily rename a script like shell.php to image.jpg. If your server trusts the type header blindly, you create a vector for arbitrary code execution or path traversal attacks. Furthermore, case sensitivity issues and empty strings in the upload form data can cause valid files to be rejected, leading to exactly the inconsistent behavior described where some users get errors while others do not.

The proposed fix in the source material suggests using mime_content_type($_FILES['fupload']['type']). This is a critical misunderstanding of how that function works. The mime_content_type() function does not read the file extension or the value stored in $_FILES['type']. Instead, it opens the actual file stream to inspect its binary magic bytes (the first few bytes of the file) and determines the MIME type based on that content.

To use this correctly, you must pass the full path to the uploaded file, not the string from the POST data. The correct implementation looks like this:

$filePath = $_FILES['fupload']['tmp_name'];
$contentType = mime_content_type($filePath);

if ($contentType === 'image/gif' || $contentType === 'image/png' || $contentType === 'image/jpeg') {
    // Safe to process the file
} else {
    // Invalid file content detected
}

By passing $tmp_name (or the full destination path after moving), mime_content_type() analyzes the actual binary signature of the file. This ensures that a file named image.jpg containing shellcode will be identified as application/x-php or similar, rather than being accepted as an image.

There is another layer of validation you should consider: checking against a whitelist using finfo. The mime_content_type() function can return results based on heuristics that might occasionally misidentify files if the magic bytes are ambiguous. A more robust approach in modern PHP involves using the finfo extension with a file MIME type database:

$filePath = $_FILES['fupload']['tmp_name'];
$finfo = new finfo(FILEINFO_MIME_TYPE);
$contentType = $finfo->file($filePath);

This method is generally considered more secure and accurate because it relies on the system's configured MIME type database rather than internal heuristics. However, you must still ensure that the resulting $contentType matches a strict whitelist of allowed image types before proceeding with processing or moving the file to its final destination.

In the context of larger frameworks like Laravel, this logic is often encapsulated within middleware or model events to keep controllers clean. If you are building a custom application without a framework, ensure that your validation happens before you move the file from the temporary directory. Once move_uploaded_file() executes, you lose access to the original stream needed for content inspection unless you handle the error state immediately.

Finally, remember that relying solely on MIME type is not enough for security. You should also validate the file extension against a whitelist and ensure the user has permission to upload only specific types. Combining magic byte analysis with extension checks and strict whitelisting creates a defense-in-depth strategy that prevents both accidental corruption and intentional exploitation of your upload form.

Our network