How to check file types of uploaded files in PHP?

How to check file types of uploaded files in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

How to check file types of uploaded files in PHP?

When handling file uploads in PHP, relying solely on the $_FILES superglobal is insufficient for security. The official documentation emphasizes using is_uploaded_file() or move_uploaded_file(), but these functions only verify that the data came from a legitimate upload stream, not what the file actually contains. If your application does not strictly require specific file types, you might get away with trusting the client entirely. However, in most real-world scenarios, users should not be allowed to upload arbitrary files for a variety of reasons.

Developers often attempt to solve this by validating MIME types or file extensions. While straightforward, these methods are notoriously flawed. The $_FILES['name']['type'] value is provided by the client and can be easily spoofed via headers like Content-Type. Similarly, checking the file extension (e.g., .jpg, .pdf) is even easier to bypass; a user can simply rename an executable script to script.jpg. Furthermore, different browsers and operating systems report MIME types inconsistently. For instance, some browsers send text/plain for files they actually treat as text/html, creating ambiguity that makes strict validation difficult without breaking legitimate uploads.

For image files specifically, there is a robust alternative: using the getimagesize() function. This approach bypasses client-side metadata entirely by attempting to read the file header. If the file claims to be an image but lacks the correct magic bytes, getimagesize() will return false. This method effectively verifies that the content matches the expected format.

$allowed_mime_types = [
    'image/jpeg' => true,
    'image/png'   => true,
    'image/gif'   => true,
];

// Attempt to get image size data
$image_info = @getimagesize($uploaded_file_path);

if ($image_info === false) {
    // File is not a valid image
    throw new InvalidArgumentException('Invalid image file.');
}

$mime_type = mime_content_type($uploaded_file_path);

if (!array_key_exists($mime_type, $allowed_mime_types)) {
    throw new InvalidArgumentException('Unsupported image format.');
}

However, getimagesize() is limited to images. What about PDFs, Word documents, Excel files, or plain text logs? For these non-image formats, you generally cannot rely on parsing headers in the same way because binary data for complex formats (like PDFs) can be large and require specific libraries to parse correctly just to identify the header.

In environments where mime_content_type() is unavailable or unreliable, the recommended approach is to utilize the Fileinfo extension. This provides a standardized way to inspect files using magic number detection without relying on system commands.

if (extension_loaded('fileinfo')) {
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $mime_type = $finfo->file($uploaded_file_path);
} else {
    // Fallback logic or throw error if extension is missing
}

If the Fileinfo extension is not loaded, a common fallback involves executing system commands like system('file -bi $uploadedfile'). While this works on many Unix-like systems, it introduces significant security risks. Running external shell commands requires careful sanitization of input to prevent command injection attacks. Additionally, as noted in discussions regarding this topic, file commands can sometimes return incorrect results depending on the system configuration or file encoding, leading to false positives or negatives.

A more secure and maintainable strategy for complex file types is often to use a dedicated validation library. For example, using the mimes package allows you to define strict allowed MIME types and extensions that map to specific file signatures, ensuring that the content matches the name regardless of what the user claims it is. This approach decouples your application logic from the inconsistencies of client headers and OS variations.

Ultimately, the goal is to validate the actual content of the uploaded file rather than trusting the metadata provided by the browser or the user. For images, getimagesize() offers a native solution; for everything else, leveraging the Fileinfo extension or specialized libraries ensures that your application remains secure against malicious file uploads without relying on brittle header checks.

Our network