php上传文件类型 mime类型大全
封面图
•
2025-09-26
•
PHP
186
0
0
0
在 PHP 中,上传文件时通常会通过 `$_FILES` 超全局数组来获取上传文件的信息,其中包括文件的 MIME 类型(即 `type` 字段)。MIME 类型用于标识文件的类型,例如 `image/png`、`application/pdf` 等。 下面是一些常见的 **MIME 类型** 列表,适用于 PHP 文件上传时的验证和处理: --- ### 📄 常见文件类型及对应的 MIME 类型 | 文件扩展名 | MIME 类型 | |------------|--------------------------| | `.3gp` | `video/3gpp` | | `.7z` | `application/x-7z-compressed` | | `.aac` | `audio/aac` | | `.avi` | `video/x-msvideo` | | `.bmp` | `image/bmp` | | `.css` | `text/css` | | `.csv` | `text/csv` | | `.doc` | `application/msword` | | `.docx` | `application/vnd.openxmlformats-officedocument.wordprocessingml.document` | | `.exe` | `application/octet-stream` | | `.gif` | `image/gif` | | `.gz` | `application/gzip` | | `.html` | `text/html` | | `.ico` | `image/x-icon` | | `.jar` | `application/java-archive` | | `.jpeg` | `image/jpeg` | | `.jpg` | `image/jpeg` | | `.js` | `application/javascript` | | `.json` | `application/json` | | `.mid` | `audio/midi` | | `.mov` | `video/quicktime` | | `.mp3` | `audio/mpeg` | | `.mp4` | `video/mp4` | | `.mpeg` | `video/mpeg` | | `.pdf` | `application/pdf` | | `.png` | `image/png` | | `.ppt` | `application/vnd.ms-powerpoint` | | `.pptx` | `application/vnd.openxmlformats-officedocument.presentationml.presentation` | | `.rar` | `application/x-rar-compressed` | | `.rtf` | `application/rtf` | | `.svg` | `image/svg+xml` | | `.tar` | `application/x-tar` | | `.tif` | `image/tiff` | | `.tiff` | `image/tiff` | | `.ts` | `video/mp2t` | | `.ttf` | `font/ttf` | | `.txt` | `text/plain` | | `.wav` | `audio/wav` | | `.webm` | `video/webm` | | `.xls` | `application/vnd.ms-excel` | | `.xlsx` | `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` | | `.xml` | `application/xml` | | `.zip` | `application/zip` | --- ### ✅ 在 PHP 中如何使用 MIME 类型进行文件上传验证? ```php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $allowed_mime_types = ['image/jpeg', 'image/png', 'application/pdf']; if (isset($_FILES['file'])) { $file = $_FILES['file']; $file_type = $file['type']; if (in_array($file_type, $allowed_mime_types)) { // 允许上传 move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']); echo "文件上传成功!"; } else { echo "不允许的文件类型。"; } } } ``` --- ### ⚠️ 注意事项 - `$_FILES['file']['type']` 是客户端提供的 MIME 类型,**不可完全信任**,因为用户可以伪造。 - 更安全的做法是结合文件内容判断(如使用 `finfo_file()`): ```php $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); if (in_array($mime_type, $allowed_mime_types)) { // 允许上传 } ``` --- 如果你需要一个完整的 MIME 类型列表,可以参考 [IANA 官方 MIME 类型注册表](https://www.iana.org/assignments/media-types/media-types.xhtml) 或使用工具库如 [PHP-Mime-Type](https://github.com/pear/PHP-Mime-Type)。 如需我提供更详细的某个类别的 MIME 类型(如音频、视频、字体等),也可以告诉我!
上一篇:
PHP数组转字符串与PHP字符串转数组的相关方法解析
下一篇:
硬链接和符号链接介绍
标题录入,一次不能超过6条
返回顶部
留言
留言
评论