HTTP Content-Type 完整參考表:200+ 擴展名對應 MIME 類型
正確設置 Content-Type 響應頭,是瀏覽器正確渲染、下載、安全策略生效的前提。本表收錄 200+ 常見文件擴展名與標準 MIME 類型映射,涵蓋文檔、圖片、音視頻、壓縮包、字體、代碼、移動應用等全品類,可作為 Nginx/Apache mime.types、PHP header()、前端 accept 屬性配置的權威參考。
核心原則
- 標準來源:IANA Media Types、RFC 2046、Apache
mime.types、Nginx mime.types
- 優先級:具體類型優於通用類型(如
application/pdf 優於 application/octet-stream)
- 字符集:文本類型建議附帶
; charset=utf-8
- 安全:用戶上傳文件務必驗證 MIME 與擴展名一致,防止 XSS/上傳繞過
完整映射表(節選高頻類別,完整版見附件)
文檔與數據
| 擴展名 | MIME 類型 | 備註 |
| .pdf | application/pdf | 標準文檔 |
| .doc/.docx | application/msword / application/vnd.openxmlformats-officedocument.wordprocessingml.document | Word |
| .xls/.xlsx | application/vnd.ms-excel / application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | Excel |
| .ppt/.pptx | application/vnd.ms-powerpoint / application/vnd.openxmlformats-officedocument.presentationml.presentation | PowerPoint |
| .txt/.csv | text/plain / text/csv | 純文本/CSV |
| .json | application/json | API 標準 |
| .xml | application/xml / text/xml | 數據交換 |
| .html/.htm | text/html | 網頁 |
| .md | text/markdown | Markdown |
| .rtf | application/rtf | 富文本 |
圖片
| 擴展名 | MIME 類型 | 備註 |
| .jpg/.jpeg | image/jpeg | 有損壓縮 |
| .png | image/png | 無損、透明 |
| .gif | image/gif | 動畫、256色 |
| .webp | image/webp | 現代格式 |
| .svg | image/svg+xml | 矢量圖 |
| .bmp | image/bmp | 位圖 |
| .ico | image/x-icon | 圖標 |
| .tiff/.tif | image/tiff | 印刷級 |
| .avif | image/avif | 下一代 |
音視頻
| 擴展名 | MIME 類型 | 備註 |
| .mp4 | video/mp4 | H.264/H.265 |
| .webm | video/webm | VP8/VP9/AV1 |
| .mp3 | audio/mpeg | 音頻 |
| .wav | audio/wav | 無損 |
| .ogg | audio/ogg / video/ogg | 開源容器 |
| .flv | video/x-flv | Flash 視頻 |
| .m3u8 | application/vnd.apple.mpegurl | HLS 串流 |
| .ts | video/mp2t | MPEG-TS 切片 |
壓縮與打包
| 擴展名 | MIME 類型 | 備註 |
| .zip | application/zip | 通用壓縮 |
| .gz | application/gzip | Gzip |
| .tar | application/x-tar | 打包 |
| .rar | application/vnd.rar | RAR |
| .7z | application/x-7z-compressed | 7-Zip |
| .bz2 | application/x-bzip2 | Bzip2 |
| .xz | application/x-xz | LZMA |
字體
| 擴展名 | MIME 類型 | 備註 |
| .woff | font/woff | Web 字體 1.0 |
| .woff2 | font/woff2 | Web 字體 2.0(更小) |
| .ttf/.otf | font/ttf / font/otf | TrueType/OpenType |
| .eot | application/vnd.ms-fontobject | IE 專用 |
代碼與腳本
| 擴展名 | MIME 類型 | 備註 |
| .js/.mjs | application/javascript / text/javascript | ES 模組用 .mjs |
| .css | text/css | 樣式表 |
| .php | application/x-httpd-php | 服務端解析 |
| .py | text/x-python | Python |
| .java | text/x-java-source | Java 源碼 |
| .sh | application/x-sh | Shell 腳本 |
| .sql | application/sql | SQL 腳本 |
移動應用
| 擴展名 | MIME 類型 | 平台 |
| .apk | application/vnd.android.package-archive | Android |
| .ipa | application/vnd.iphone | iOS |
| .aab | application/vnd.android.appbundle | Android App Bundle |
| .xap | application/x-silverlight-app | Windows Phone (舊) |
其他常見
| 擴展名 | MIME 類型 | 備註 |
| .csv | text/csv | 逗號分隔值 |
| .ics | text/calendar | 日曆 |
| .vcf | text/vcard | 聯繫人 |
| .epub | application/epub+zip | 電子書 |
| .torrent | application/x-bittorrent | BitTorrent |
| .* (未知) | application/octet-stream | 強制下載 |
PHP 設置範例
<?php
// 根據擴展名輸出正確 Content-Type
function setContentType($filename) {
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$map = [
'pdf' => 'application/pdf',
'jpg' => 'image/jpeg',
'png' => 'image/png',
'json' => 'application/json',
'csv' => 'text/csv',
'zip' => 'application/zip',
'apk' => 'application/vnd.android.package-archive',
// ... 更多映射
];
$type = $map[$ext] ?? 'application/octet-stream';
header("Content-Type: $type");
// 文本類型建議加 charset
if (str_starts_with($type, 'text/') || $type === 'application/json') {
header("Content-Type: $type; charset=utf-8");
}
}
Nginx/Apache 配置提示
- Nginx:
include mime.types; 在 http 塊,默認已含 800+ 映射
- Apache:
AddType application/pdf .pdf 或編輯 mime.types 文件
- 自定義類型:
types { application/wasm wasm; } (Nginx)
安全最佳實踐
- 上傳驗證:
finfo_file(fopen($tmp, 'r'), FILEINFO_MIME_TYPE) 驗證真實 MIME
- 擴展名白名單:僅允許業務必要類型
- 下載響應:
Content-Disposition: attachment; filename="..." 強制下載
- 防止嗅探:
X-Content-Type-Options: nosniff
將此表作為專案 docs/mime-types.md 存檔,前後端協作、Nginx 配置、文件上傳下載、API 設計時隨時查閱,確保 Content-Type 永遠精準。