我无法让我的broswer显示数据库中的图像(存储为blob)。我试过了 header(“Content-type: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
));
答案 0 :(得分:2)
在数据库中存储图像数据时,不应使用addslashes()。更好的选择是在输出图像数据时使用base64_encode()和base64_decode()。一个简单的搜索将找到很多这方面和类似问题的好答案