🍋
Menu
Troubleshooting Beginner 2 min read 313 words

Troubleshooting File Extension and MIME Type Mismatches

When a file won't open or opens in the wrong application, the issue is often a mismatch between the file extension, MIME type, and actual file content.

Understanding the Mismatch

A file has three identity layers: its extension (.jpg, .pdf), its MIME type (image/jpeg, application/pdf), and its actual binary content (the magic bytes at the start of the file). When these disagree, problems occur.

Common Mismatch Scenarios

A JPEG image saved with a .png extension — image viewers may display it correctly (they check magic bytes) but web servers may send the wrong Content-Type header, causing browser confusion. A CSV file with a .txt extension — double-clicking opens it in a text editor instead of a spreadsheet application. An HTML file served with Content-Type: text/plain — the browser displays the source code instead of rendering the page.

How to Identify True File Type

On macOS/Linux, the file command examines magic bytes to identify the actual file type regardless of extension: file mystery_document.xyz might return "PDF document, version 1.5". Online tools that check file signatures can also identify mislabeled files. In a hex editor, the first few bytes reveal the file format: JPEG starts with FF D8 FF, PNG with 89 50 4E 47, PDF with 25 50 44 46.

Web Server MIME Type Issues

Incorrect MIME types in web server configuration cause browsers to handle files incorrectly. Ensure your web server maps extensions to correct MIME types. Critical mappings: .js → application/javascript (not text/javascript), .css → text/css, .svg → image/svg+xml, .webp → image/webp, .woff2 → font/woff2. Missing MIME types for modern formats (AVIF, WebP) are a common issue on older servers.

Fixing Mismatches

For files with wrong extensions: identify the true type and rename. For web server issues: configure MIME types in nginx.conf, .htaccess, or your web framework. For email attachment issues: rename files to their correct extension before sending. For files that won't open at all: check if the file is corrupted by comparing its size to expected values and checking its magic bytes.

相关工具

相关格式

相关指南