无法使用PHP显示数据库中的blob图像


Cannot display blob image from database using PHP

我无法让浏览器显示数据库中的图像(存储为blob)。我试过了header("内容类型:image/jpeg");echo";

但它仍然无法在浏览器上显示。

getImage.php

<?php
require_once 'php-activerecord/ActiveRecord.php';
ActiveRecord'Config::initialize(function($cfg) {
    $cfg->set_model_directory('models');
    $cfg->set_connections(array(
        'development' => 'mysql://root:mysql@localhost/BondingTogether'));
});
$id = addslashes($_REQUEST['id']);
$row = food::find_by_foodid($id);
$image = $row->image;
//$image = ""

//header("Content-Type: image/jpg");
header("Content-type: image/jpeg");
//echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo $image;
//echo base64_decode($image);

添加到数据库

<?php
require_once 'php-activerecord/ActiveRecord.php';
ActiveRecord'Config::initialize(function($cfg) {
    $cfg->set_model_directory('models');
    $cfg->set_connections(array(
        'development' => 'mysql://root:mysql@localhost/BondingTogether'));
});
//files
$file = $_FILES['foodimage']['tmp_name'];
if (!isset($file)) {
} 
else {
    //$image = addslashes(file_get_contents($_FILES['foodimage']['tmp_name']));
    $image_name = addslashes($_FILES['foodimage']['tmp_name']);
    $image_size = getimagesize($_FILES['foodimage']['tmp_name']);
    if ($image_size == FALSE) {
        die('Please select an image file');
    } else {
    }
}
$image = addslashes(file_get_contents($_FILES['foodimage']['tmp_name']));
//$image = chunk_split(base64_encode(file_get_contents("image.jpg")));
Food::create(array(
    'xcoord' => $_POST['XCoord']
    , 'ycoord' => $_POST['YCoord']
    , 'title' => $_POST['title']
    , 'category' => $_POST['cat']
    , 'description' => $_POST['desc']
    , 'image' => $image
));

在数据库中存储图像数据时,不应该使用addslashes()。一个更好的选择是在输出图像数据时使用base64_encode()和base64_decode()插入图像数据。一个简单的搜索就会找到很多关于这个和类似问题的好答案