Determine mime type from MySQL column

I got the exported database from MSAccess (not my favorite) and I imported it into a MySQL table. There is a column named "customerImage" and is a "long BLOB" with the attribute "binary". How to determine the type of mime? I tried different methods, but they should all be a file, but data.

If someone can help me with the PHP code or the MySQL command, that would be great.

+3
source share
4 answers

If your host still uses php 5.2 and does not have access to fileinfo functions, you can check the signature of the file header (magic numbers) to determine the 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 4 java- (.jar), foreach, mime, .

http://www.garykessler.net/library/file_sigs.html, - .

+4

FileInfo , , finfo_buffer() ():

.

.


: PHP >= 5.3

+3

Save the blob in a temporary file and use the php finfo_file function .

+1
source

You can use the FileInfo extension . It contains a function calledfinfo_buffer()

0
source

All Articles