使用php显示存储在数据库中的图像(tinyblob)

I try to read image from database (blob) but i have problem becouse i don't know mime type of image. I have only tinyblob.

Can i read image and save it back to my hard disk?

The best solution is to store the mime-type in the DB at the same time you're inserting the image into the blob field. Otherwise you're going to have to the following EACH TIME the image is retrieved:

$image = $row['imageblob'];  // $row = result row from DB query.

$finfo = new finfo(FILEINFO_MIME);
$mime_type = $finfo->buffer($image);

This gets to be expensive very quickly on a busy system, so best do the determination ONCE and then store that result.

Relevant PHP docs here.

Why not store the images on the hard disk all the time, and store in the Database a relative link based on a known directory?

Here's some code that I have used to get a logo (blob) from a mysql database

<a href="index.php"><img src="data:image/png;base64,<?php echo base64_encode($MyClass->getLogo())?>" alt="Logo" width="233" height="65" /></a>

And the getLogo() function

 public function getLogo()
    {
        if ($this->getId())
            $query = "SELECT `logo` FROM Logos WHERE `logo_id` = '{$this->getId()}' LIMIT 1";
        else
            $query = "SELECT `logo` FROM Logos WHERE `logo_id` = '1' LIMIT 1";

        $result = mysql_query($query);
        if ($result)
            $row = mysql_fetch_array($result);
        else
            return NULL;                    

        return ($row['logo']);
    }