从MySQL列中确定MIME类型 [英] Determinate mime type from MySQL column

查看:98
本文介绍了从MySQL列中确定MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MSAccess(不是我的最爱)接收到导出的数据库,并将其导入到MySQL表中.有一个名为"customerImage"的列,是一个以"binary"为属性的"long BLOB"类型.如何确定Mime类型?我尝试了不同的方法,但所有方法都必须是文件,但必须是数据.

I received an exported database from MSAccess (not my favorite) and I imported it to a MySQL table. There's a column named 'customerImage' and is a 'long BLOB' type with 'binary' as attribute. How can I determinate the Mime Type? I've tried different methods but all of them requires to be a file but data.

如果有人可以帮助我处理PHP代码或MySQL命令,那就太好了.

If someone could help me with PHP code or MySQL command would be great.

推荐答案

如果您的主机仍使用php 5.2并且没有访问fileinfo函数的权限,则可以测试文件头签名(魔术数字)以确定MIME类型

IF your host still uses php 5.2 and dont have access to the fileinfo functions you can test the files header signature (magic numbers) to determine mime type

function mimetype($data)
{
    //File signatures with their associated mime type
    $Types = array(
    "474946383761"=>"image/gif",                        //GIF87a type gif
    "474946383961"=>"image/gif",                        //GIF89a type gif
    "89504E470D0A1A0A"=>"image/png",
    "FFD8FFE0"=>"image/jpeg",                           //JFIF jpeg
    "FFD8FFE1"=>"image/jpeg",                           //EXIF jpeg
    "FFD8FFE8"=>"image/jpeg",                           //SPIFF jpeg
    "25504446"=>"application/pdf",
    "377ABCAF271C"=>"application/zip",                  //7-Zip zip file
    "504B0304"=>"application/zip",                      //PK Zip file ( could also match other file types like docx, jar, etc )
    );

    $Signature = substr($data,0,60); //get first 60 bytes shouldnt need more then that to determine signature
    $Signature = array_shift(unpack("H*",$Signature)); //String representation of the hex values

    foreach($Types as $MagicNumber => $Mime)
    {
        if( stripos($Signature,$MagicNumber) === 0 )
            return $Mime;  
    }

    //Return octet-stream (binary content type) if no signature is found
    return "application/octet-stream"; 
}

注意::某些签名可能与其他签名匹配,例如PK Zip文件签名与Java存档(.jar)文件签名的前4个字节匹配,因此需要额外的语句在foreach循环中确定适合哑剧类型的正确签名,但是对于您的情况,应该这样做.

NOTE: Some signatures may match partials of others, for instance the PK Zip file signature matches the first 4 bytes of java archive (.jar) file signature, extra statements would be needed in the foreach loop to determine the correct signature for the mime type, but for your situation this should do.

可以在 http://www.garykessler.net/上找到文件签名的更新列表. library/file_sigs.html (如果有人需要更多文件签名类型).

A updated list of file signatures can be found at http://www.garykessler.net/library/file_sigs.html if someone needs more file signature types.

这篇关于从MySQL列中确定MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆