Reputation: 21
I am not able to get my broswer to display an image from the database(stored as blob). I have tried header("Content-type: image/jpeg"); echo '';
But it is still not able to display on the browser.
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
));
Upvotes: 0
Views: 1078
Reputation: 21
You should not be using addslashes() when storing image data in your DB. A better alternative is to insert the image data with base64_encode(), and base64_decode() it when you output it. A simple search will find plenty of good answers to this and similar questions
Upvotes: 2