常见 MimeType 参考表

媒体类型文件扩展名说明
application/msworddoc微软 Office Word 格式(Microsoft Word 97 – 2004 document)
application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx微软 Office Word 文档格式
application/vnd.ms-excelxls微软 Office Excel 格式(Microsoft Excel 97 – 2004 Workbook
application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx微软 Office Excel 文档格式
application/vnd.ms-powerpointppt微软 Office PowerPoint 格式(Microsoft PowerPoint 97 – 2003 演示文稿)
application/vnd.openxmlformats-officedocument.presentationml.presentationpptx微软 Office PowerPoint 文稿格式
application/x-gzipgz, gzipGZ 压缩文件格式
application/zipzip, 7zipZIP 压缩文件格式
application/rarrarRAR 压缩文件格式
application/x-tartar, tgzTAR 压缩文件格式
application/pdfpdfPDF 是 Portable Document Format 的简称,即便携式文档格式
application/rtfrtfRTF 是指 Rich Text Format,即通常所说的富文本格式
image/gifgifGIF 图像格式
image/jpegjpg, jpegJPG(JPEG) 图像格式
image/jp2jpg2JPG2 图像格式
image/pngpngPNG 图像格式
image/tifftif, tiffTIF(TIFF) 图像格式
image/bmpbmpBMP 图像格式(位图格式)
image/svg+xmlsvg, svgzSVG 图像格式
image/webpwebpWebP 图像格式
image/x-iconicoico 图像格式,通常用于浏览器 Favicon 图标
application/kswpswps金山 Office 文字排版文件格式
application/ksetet金山 Office 表格文件格式
application/ksdpsdps金山 Office 演示文稿格式
application/x-photoshoppsdPhotoshop 源文件格式
application/x-coreldrawcdrCoreldraw 源文件格式
application/x-shockwave-flashswfAdobe Flash 源文件格式
text/plaintxt普通文本格式
application/x-javascriptjsJavascript 文件类型
text/javascriptjs表示 Javascript 脚本文件
text/csscss表示 CSS 样式表
text/htmlhtm, html, shtmlHTML 文件格式
application/xhtml+xmlxht, xhtmlXHTML 文件格式
text/xmlxmlXML 文件格式
text/x-vcardvcfVCF 文件格式
application/x-httpd-phpphp, php3, php4, phtmlPHP 文件格式
application/java-archivejarJava 归档文件格式
application/vnd.android.package-archiveapkAndroid 平台包文件格式
application/octet-streamexeWindows 系统可执行文件格式
application/x-x509-user-certcrt, pemPEM 文件格式
audio/mpegmp3mpeg 音频格式
audio/midimid, midimid 音频格式
audio/x-wavwavwav 音频格式
audio/x-mpegurlm3um3u 音频格式
audio/x-m4am4am4a 音频格式
audio/oggoggogg 音频格式
audio/x-realaudioraReal Audio 音频格式
video/mp4mp4mp4 视频格式
video/mpegmpg, mpe, mpegmpeg 视频格式
video/quicktimeqt, movQuickTime 视频格式
video/x-m4vm4vm4v 视频格式
video/x-ms-wmvwmvwmv 视频格式(Windows 操作系统上的一种视频格式)
video/x-msvideoaviavi 视频格式
video/webmwebmwebm 视频格式
video/x-flvflv一种基于 flash 技术的视频格式

PHP 获取文件 MIME 方法如下:

// 获取文件 MIME 类型
function get_mime_type($file) {
	if (function_exists('finfo_open')) {
		$finfo = finfo_open(FILEINFO_MIME_TYPE);
		$mimetype = finfo_file($finfo, $file);
		finfo_close($finfo);
	} else {
		$mimetype = mime_content_type($file);
	}

	if (empty($mimetype)) {
		$mimetype = 'application/octet-stream';
	}

	return $mimetype;
}

Javascript

Javascript 作为使用最广泛的脚本语言之一,在客户端验证文件类型是一种常见的场景。下面是通过 JS 查看文件类型的方法。

首先,在页面上定义一个文件上传控件:

<input type="file" id="fileUploader" multiple>

获取文件类型方法如下:

// 获取上传控件
var uploader = document.getElementById("fileUploader");

// 监听上传控件的 change 事件
uploader.addEventListener("change", function(event) {
    var files = uploader.files,
    for (var i = 0; i < files.length; i++) {
        console.log("文件名:" + files[i].name);
        console.log("文件类型" + files[i].type);
        console.log("文件大小" + files[i].size + " 字节");
    }
}, false);