代码拉取完成,页面将自动刷新
<?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));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。