返回顶部

收藏

获取文件MIME类型的php代码

更多
<?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*/

标签:php

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. ksc 发表 2014-05-09 13:41:26 php模版引擎 模版继承{extends}的简单实现
  2. lerry 发表 2011-04-08 16:42:00 在32位ubuntu server下编译带php-fpm的php
  3. hqlulu <> 发表 2017-11-10 04:20:34 CI的session功能在PHP7.1下不可用
  4. hqlulu <> 发表 2017-11-15 06:20:01 phpMyadmin设置自动连接数据库
  5. thinkpc 发表 2017-11-22 16:58:49 Curl&nbsp;PHP&nbsp;77&nbsp;错误码&nbsp;CURLE_SSL_CACE
  6. 博主 发表 2017-08-04 06:42:00 PHP 的错误和异常处理机制
  7. 博主 发表 2017-08-07 09:02:00 PHP 的数据结构扩展
  8. 博主 发表 2017-08-08 02:51:00 PHP7 安装 xhprof
  9. 博主 发表 2017-09-22 10:48:00 PHP headers already sent 原因分析
  10. Leniy 发表 2017-11-26 09:31:19 修复博客中的php notice告警
  11. admin 发表 2017-12-05 11:06:59 Linux下全局安装composer方法
  12. wwek 发表 2017-07-14 09:10:16 简单粗暴,专门解决PHP MYSQL数据库雪崩

发表评论