php准确获取文件MIME类型的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-28 20:35:06 浏览: 评论:0
这篇文章主要介绍了php准确获取文件MIME类型的方法,涉及php针对文件属性操作的相关技巧,需要的朋友可以参考下。
本文实例讲述了php准确获取文件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 (!emptyempty($ext) AND !emptyempty($mime[$ext])) {
- $type[0] = $mime[$ext];
- }
- }
- return $type[0];
- }
- $path = '1.txt'; //实际上当前路径并不存在1.txt
- var_dump(_getMimeType($path));
- /*End of php*/
Tags: php获取文件MIME
相关文章
- ·php实现获取文件mime类型的方法(2021-05-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)