How do I find the MIME type of a file with PHP?

How do I find the MIME type of a file with PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Determining MIME Types in PHP Without Relying on finfo

When building a PHP application that proxies requests to static files via index.php, determining the correct MIME type is often more critical than simply verifying the file exists. Your current setup includes:

include('/www/site'.$_SERVER['REQUEST_URI']);

While this successfully serves the file content, browsers rely on the Content-Type HTTP header to decide how to render that data. If you serve a JPEG as text/plain, modern browsers will display a broken image icon instead of rendering the photo. The core challenge is mapping the requested filename (or URI) to its appropriate MIME type without assuming the user's intent or trusting client headers blindly.

Inspecting the File Extension

The most straightforward approach for static assets like images, PDFs, and documents is to inspect the file extension. You can extract this using pathinfo():

$uri = $_SERVER['REQUEST_URI'];
$pathInfo = pathinfo($uri);

if (isset($pathInfo['extension'])) {
    $fileType = strtolower($pathInfo['extension']);
} else {
    $fileType = '';
}

Once you have the extension, you can map it to a MIME type using a lookup array. This is safe, predictable, and requires no external dependencies:

$mimeTypeMap = [
    'jpg' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'png' => 'image/png',
    'gif' => 'image/gif',
    'pdf' => 'application/pdf',
    'txt' => 'text/plain',
    'css' => 'text/css',
    'js' => 'application/javascript',
];

$mime = $mimeTypeMap[$fileType] ?? 'application/octet-stream';
header('Content-Type: ' . $mime);
include('/www/site'.$uri);

This method works well for known file types. However, it fails for files without extensions or with misleading ones (e.g., a .jpg that is actually an image inside a ZIP archive).

Using finfo to Inspect File Content

If you need to detect the MIME type based on the actual file content rather than the extension, PHP's built-in finfo module is the standard tool. This is often necessary for files uploaded by users or served without extensions.

To use it, you must first initialize a finfo resource with the appropriate magic database:

$fp = finfo_open(FILEINFO_MIME_TYPE);
$contentType = finfo_file($fp, '/www/site'.$uri);
finfo_close($fp);

However, there is a catch. The include statement reads the file into memory before you can stream it back to the client efficiently for large files. A better pattern is to open the file handle first, check the type, then stream the content:

$filePath = '/www/site'.$uri;

// Ensure the file exists and is readable
if (!file_exists($filePath)) {
    http_response_code(404);
    exit;
}

$fp = finfo_open(FILEINFO_MIME_TYPE);
$contentType = finfo_file($fp, $filePath);
finfo_close($fp);

header('Content-Type: ' . $contentType);
readfile($filePath);
exit;

Handling Edge Cases and Security

Relying solely on extensions is generally safer than trusting finfo results blindly. While finfo is accurate, malicious actors might upload a file with a double extension (e.g., shell.php.jpg) hoping the server ignores the extension and serves it as an image. If you use finfo, ensure your magic database is up to date, or consider validating against a strict allowlist of known extensions for sensitive directories.

Additionally, be aware that finfo might return application/octet-stream if it cannot determine the type from the magic bytes. Always have a fallback in your logic to prevent serving unknown binary data as executable scripts.

For large-scale applications or those requiring dynamic content negotiation, frameworks like Laravel handle MIME types automatically based on file paths and extensions. If you are starting a new project, leveraging such tools can reduce the boilerplate needed for this specific logic.

Ultimately, the decision between extension-based mapping and content-based detection depends on your trust model. For public-facing static assets, stick to strict extension validation. For user-uploaded files, combine extension checks with finfo verification to ensure the server serves what it thinks it is serving.

Our network