1 Star 0 Fork 0

IMue/PHPCommonUtil

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MimetypeUtil.php 5.38 KB
一键复制 编辑 原始数据 按行查看 历史
DXCyber409 提交于 2017-10-15 17:16 . v201710151716
<?php
namespace util\Dxcyber409;
/** MimetypeUtil class, 用于检测文件的mime类型
* Date: 2013-09-25
* Author: DXCyber409
* Ver: 1.0
*
* Func:
* public get_mime_typeName 获取文件mime类型
*/
class MimetypeUtil {
//定义mime类型,当外部函数失效时备用
private $mimeType = array(
//applications(应用类型)
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'exe' => 'application/octet-stream',
'doc' => 'application/vnd.ms-word',
'xls' => 'application/vnd.ms-excel',
'pdf' => 'application/pdf',
'xml' => 'application/xml',
'ppt' => 'application/vnd.ms-powerpoint',
'pps' => 'application/vnd.ms-powerpoint',
'odt' => 'application/vnd.oasis.opendocument.text',
'swf' => 'application/x-shockwave-flash',
//archives(档案类型)
'gz' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'zip' => 'application/zip',
'rar' => 'application/x-rar',
'tar' => 'application/x-tar',
'bz' => 'application/x-bzip2',
'bz2' => 'application/x-bzip2',
'tbz' => 'application/x-bzip2',
'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',
'pl' => 'text/x-perl',
'sql' => 'text/x-sql',
'rb' => 'text/x-ruby',
'sh' => 'text/x-shellscript',
//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'
);
/**
* 侦测调用函数
* @return string
*/
private function check_function_type() {
if (class_exists('finfo')) {
return 'finfo';
} else if (function_exists('mime_content_type')) {
return 'mime_content_type';
} else if (function_exists('exec')) {
//linux
$result = exec('file -ib ' . escapeshellarg(__FILE__));
if (0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
return 'linux';
}
//unix
$result = exec('file -Ib ' . escapeshellarg(__FILE__));
if (0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
return 'bsd';
}
}
return 'internal';
}
/**
* 获取文件mime函数
* @return string 返回mime类型
*/
public function get_mime_typeName($filePath) {
//获取函数类型
$filemime = $this->check_function_type();
//分支判断
switch ($filemime) {
case 'finfo':
$finfo = finfo_open(FILEINFO_MIME);
if ($finfo) {
$type = @finfo_file($finfo, $filePath);
}
break;
case 'mime_content_type':
$type = mime_content_type($filePath);
break;
case 'linux':
$type = exec('file -ib ' . escapeshellarg($filePath));
break;
case 'bsd':
$type = exec('file -Ib ' . escapeshellarg($filePath));
break;
default:
$pinfo = pathinfo($filePath);
$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
$type = isset($this->mimeType[$ext]) ? $this->mimeType[$ext] : 'unkown';
break;
}
//将以分号连接的字符串分割为数组
$type = explode(';', $type);
//防止mime_content_type函数获取一个不存在的文件,而返回'application/octet-stream'类型
if ($filemime != 'internal' && $type[0] == 'application/octet-stream') {
$pinfo = pathinfo($filePath);
$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
if (!empty($ext) && !empty($this->mimeType[$ext])) {
$type[0] = $this->mimeType[$ext];
}
}
return $type[0];
}
/**
* 使用示例
*/
public function demo() {
//网站根目录路径,自行改名实际存在的文件运行
$filepath = $_SERVER['DOCUMENT_ROOT'] . '/test.php';
$filepath2 = $_SERVER['DOCUMENT_ROOT'] . '/123.jpg';
$mimeUtil = new MimetypeUtil();
var_dump($mimeUtil->get_mime_typeName($filepath));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/DXCyber409/PHPCommonUtil.git
git@gitee.com:DXCyber409/PHPCommonUtil.git
DXCyber409
PHPCommonUtil
PHPCommonUtil
master

搜索帮助