Php longblob到img

Php longblob到img,php,database,Php,Database,我有一个函数,它调用一个数据库类,并要求提供一个图像列表。 下面是函数 //Hämtar en array av filer från disk. public function GetFiles() { $sql = "SELECT pic FROM pictures"; $stmt = $this->database->Prepare($sql); $fileList = $this->database->GetAll($stmt);

我有一个函数,它调用一个数据库类,并要求提供一个图像列表。 下面是函数

//Hämtar en array av filer från disk.
public function GetFiles()
{   

    $sql = "SELECT pic FROM pictures";
    $stmt = $this->database->Prepare($sql);

    $fileList = $this->database->GetAll($stmt);
    echo $fileList;
    if($fileList)
    {
        return $fileList;
    }
    return false;
}
这是GetFiles调用的数据库类方法

public function GetAll($sqlQuery) {

        if ($sqlQuery === FALSE)
        {
            throw new \Exception($this->mysqli->error);
        }
        //execute the statement
        if ($sqlQuery->execute() == FALSE)
        {
            throw new \Exception($this->mysqli->error);
        }
        $ret = 0;
        if ($sqlQuery->bind_result($ret) == FALSE)
        {
            throw new \Exception($this->mysqli->error);
        }

        $data = array();
        while ($sqlQuery->fetch())
        {
            $data[] = $ret;
            echo $ret;
        }

        $sqlQuery->close();

        return $data;
    }
GetFiles函数返回值随后由另一个函数处理

public function FileList($fileList)
{
    if(count($fileList) == 0)
    {
        return "<p class='error'> There are no files in array</p>";
    }

    $list = '';
    $list .= "<div class='list'>";
    $list .= "<h2>Uploaded images</h2>";
    foreach ($fileList as $file) {

        $list .= "<img src=".$file." />";
    }
    $list .= "</div>";
    return $list;
}
公共函数文件列表($FileList)
{
如果(计数($fileList)==0)
{
返回“

数组中没有文件”

; } $list=''; $list.=“”; $list.=“上传的图像”; foreach($fileList作为$file){ $list.=“”; } $list.=“”; 返回$list; }

但是,我的数据库只是以许多Carachter的形式返回longblob,如何使longblob显示为图像?

如果将其作为网页的一部分嵌入,则应使用


最好是让
标记指向一个PHP文件,该文件设置了正确的
内容类型
标题并转储图像数据。

您需要对其进行base64编码,并通过一个数据URI传入,例如

<img src="data:image/jpeg;base64,<?php echo base64_encode($file) ?>" />

像这样的问题就是为什么从数据库中提供图像通常不是一个好主意。

我建议您将图像保存到文件系统并从那里加载,原因如下:

  • 它在数据库上很昂贵,而且数据库更难扩展
  • 它使用许多系统资源来完成一项次要任务
  • InternetExplorer,尤其是IE8,将为长度超过34k的src提供错误
研究CDN选项

如果你坚持

$list  = sprintf("<img src=\"data:image/jpeg;base64,%s\" />",base64_encode($file));    
$list=sprintf(“,base64_encode($file));

保存和加载图片需要什么,我该怎么做,我是php新手..sorry1。不要将图像保存到MySQL,它已被保存。。将其复制到缓存文件夹并仅检索新图像。。如果你需要帮助,请告诉我。。。。
$blob = get_image_data($_GET[xxx]);
header('Content-type: image/jpeg');
echo $blob;
$list  = sprintf("<img src=\"data:image/jpeg;base64,%s\" />",base64_encode($file));