简体   繁体   English

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

[英]Cannot display blob image from database using PHP

I am not able to get my broswer to display an image from the database(stored as blob). 我无法使浏览器显示数据库中的图像(存储为Blob)。 I have tried header("Content-type: image/jpeg"); 我已经尝试过header(“ Content-type:image / jpeg”); echo ''; 回声'';

But it is still not able to display on the browser. 但是它仍然无法在浏览器上显示。

getImage.php 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);

Adding to the database 添加到数据库

<?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
));

You should not be using addslashes() when storing image data in your DB. 将图像数据存储在数据库中时,不应使用addslashes()。 A better alternative is to insert the image data with base64_encode(), and base64_decode() it when you output it. 更好的选择是在输出图像数据时使用base64_encode()和base64_decode()插入图像数据。 A simple search will find plenty of good answers to this and similar questions 一个简单的搜索将找到很多有关此问题和类似问题的好答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM