首页 > php教程 > PHP源码 > 正文

文件MIME类型获取

PHP中文网
发布: 2016-05-25 17:13:22
原创
1221人浏览过

文件mime类型获取              

<?php
$mime = array (
		//applications
		'ai'    => 'application/postscript',
		'eps'   => 'application/postscript',
		'exe'   => 'application/octet-stream',
		'doc'   => 'application/vnd.ms-word',
		'xls'   => 'application/vnd.ms-excel',
		'ppt'   => 'application/vnd.ms-powerpoint',
		'pps'   => 'application/vnd.ms-powerpoint',
		'pdf'   => 'application/pdf',
		'xml'   => 'application/xml',
		'odt'   => 'application/vnd.oasis.opendocument.text',
		'swf'   => 'application/x-shockwave-flash',
		// archives
		'gz'    => 'application/x-gzip',
		'tgz'   => 'application/x-gzip',
		'bz'    => 'application/x-bzip2',
		'bz2'   => 'application/x-bzip2',
		'tbz'   => 'application/x-bzip2',
		'zip'   => 'application/zip',
		'rar'   => 'application/x-rar',
		'tar'   => 'application/x-tar',
		'7z'    => 'application/x-7z-compressed',
		// texts
		'txt'   => 'text/plain',
		'php'   => 'text/x-php',
		'html'  => 'text/html',
		'htm'   => 'text/html',
		'js'    => 'text/javascript',
		'css'   => 'text/css',
		'rtf'   => 'text/rtf',
		'rtfd'  => 'text/rtfd',
		'py'    => 'text/x-python',
		'java'  => 'text/x-java-source',
		'rb'    => 'text/x-ruby',
		'sh'    => 'text/x-shellscript',
		'pl'    => 'text/x-perl',
		'sql'   => 'text/x-sql',
		// images
		'bmp'   => 'image/x-ms-bmp',
		'jpg'   => 'image/jpeg',
		'jpeg'  => 'image/jpeg',
		'gif'   => 'image/gif',
		'png'   => 'image/png',
		'tif'   => 'image/tiff',
		'tiff'  => 'image/tiff',
		'tga'   => 'image/x-targa',
		'psd'   => 'image/vnd.adobe.photoshop',
		//audio
		'mp3'   => 'audio/mpeg',
		'mid'   => 'audio/midi',
		'ogg'   => 'audio/ogg',
		'mp4a'  => 'audio/mp4',
		'wav'   => 'audio/wav',
		'wma'   => 'audio/x-ms-wma',
		// video
		'avi'   => 'video/x-msvideo',
		'dv'    => 'video/x-dv',
		'mp4'   => 'video/mp4',
		'mpeg'  => 'video/mpeg',
		'mpg'   => 'video/mpeg',
		'mov'   => 'video/quicktime',
		'wm'    => 'video/x-ms-wmv',
		'flv'   => 'video/x-flv',
		'mkv'   => 'video/x-matroska'
		);

function _getMimeDetect() {
	if (class_exists('finfo')) {
		return 'finfo';
	} else if (function_exists('mime_content_type')) {
		return 'mime_content_type';
	} else if ( function_exists('exec')) {
		$result = exec('file -ib '.escapeshellarg(__FILE__));
		if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
			return 'linux';
		}
		$result = exec('file -Ib '.escapeshellarg(__FILE__));
		if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
			return 'bsd';
		}
	}
	return 'internal';
}

function _getMimeType($path) {
	global $mime;
	$fmime = _getMimeDetect();
	switch($fmime) {
		case 'finfo':
			$finfo = finfo_open(FILEINFO_MIME);
			if ($finfo) 
				$type = @finfo_file($finfo, $path);
			break;
		case 'mime_content_type':
			$type = mime_content_type($path);
			break;
		case 'linux':
			$type = exec('file -ib '.escapeshellarg($path));
			break;
		case 'bsd':
			$type = exec('file -Ib '.escapeshellarg($path));
			break;
		default:
			$pinfo = pathinfo($path);
			$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
			$type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
			break;
	}
	$type = explode(';', $type);
	
	//需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream'
	if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
		$pinfo = pathinfo($path); 
		$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
		if (!empty($ext) AND !empty($mime[$ext])) {
			$type[0] = $mime[$ext];
		}
	}
	
	return $type[0];
}

$path = '1.txt';  //实际上当前路径并不存在1.txt
var_dump(_getMimeType($path));

/*End of php*/
登录后复制

 以上就是文件MIME类型获取的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Shakespeare
Shakespeare

一款人工智能文案软件,能够创建几乎任何类型的文案。

Shakespeare 49
查看详情 Shakespeare

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号