not able to fetch image from blob in mysql using php - php

Code for uploading in the blob :
mysql_connect('localhost','root','');
mysql_select_db("dtbase");
$file = $_FILES['logo']['tmp_name'];
echo $file;
$imgData =addslashes (file_get_contents($_FILES['logo']['tmp_name']));
$sql="insert into tab1(coname,date,volume,num,eissn,jname,info1,info2,section,logo)values('$coname','$date','$volume','$num','$eissn','$jname','$info1','$info2','$section','{$imgData}')";
$rs=mysql_query($sql);
Code for retrieving:
$image = mysql_query("SELECT * FRom tab1 WHERE coname='$coname'");
$im = mysql_fetch_assoc($image);
$img = $im['logo'];
Using $img to show the image:
<img src="$img" />
The image does not appear on the web page.

Try this:
echo '<img src="data:image/jpeg;base64,'.base64_encode($im['logo']).'" alt="photo"><br>';

Related

Display img from database with php not working

I managed to upload images into a database(lonblob). I have issues display that image:
Upload:
if(isset($_POST['pic_upload'])){
if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
echo "Please select an image.";
}else{
$image= addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image);
}
}
saveimage(writes the $image into a db field(type longblob))
Get images:
function get_image($userid){
$sql = "SELECT * FROM images WHERE user_id = ".$userid."";
$result = execute_sql($sql);
return $result;
}
display image:
<?php
$row = mysqli_fetch_array($img);
echo '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode($row[3]).'">';
?>
The upload works perfectly, but the image doesn't appear on the site.
You're encoding it twice.
<?php
$row = mysqli_fetch_array($img);
echo '<img height="300" width="300" src="data:image/jpeg;base64,'.$row[3].'">';
?>
Also, I would recommend looking at pathinfo(), finding the extension a file on upload?

Can't display image after uploading it to a data base, PHP and Mysql

I need to do a web page for a client to upload images to a data base and display them.
I am achieve to upload the images into a database, but I'm having trouble displaying them, but I can't work out why
Here is my code:
<!DOCTYPE html>
<head>
<body>
<form action="form.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="image" /> <input type="submit" value="Upload" />
</form>
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test" ) or die(mysql_error());
$file = $_FILES['image'] ['tmp_name'];
if (!isset($file)) {
echo "<br>Please select an image.";
}
else {
$image = addslashes(file_get_contents($_FILES['image'] ['tmp_name']));
$imageName = addslashes($_FILES['image']['name']);
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE)
echo "<br><br>Thats not an image. <br><br>";
else{
if (!$insert = mysql_query("INSERT INTO imgup VALUES ('','$imageName','$image')"))
echo "Problem uploading the image.";
else{
$lastId = mysql_insert_id();
echo "Article uploaded.<p /> Your image:<p /> <img src=get.php?id=$lastId>";
}
}
}
?>
</body>
</html>
This is my file who turn the image blob into an image:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test" ) or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM blog WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
And at the end the image does not display and this is what i get: http://goo.gl/gi1Uuc
And if i go and check my database, the image has ben successfully uploaded...
Depending on the file use inline base64 encoding. This is done with:
echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';
Font: base64_encode
OR
Put the T upperCase (Type), because can giving error in IE. Try printing with the function file_get_contents.
header('Content-Type: image/jpeg');
echo readfile($image);
I wouldn't store any image in a database. You should save it as file, and store the file's name in the database. You can then configure which directory an image gets served from without worrying about the full path to the image, or storing binary data in your db (yuck).
Try changing:
$image = mysql_query("SELECT * FROM blog WHERE id=$id");
to:
$image = mysql_query("SELECT * FROM blog WHERE id = '$id'");
Escaping an image file with addslashes will probably corrupt it, the imagesize test should be sufficient

Error in displaying an image in php mysql

Hey I am facing a problem in displaying images in php. The images are being stored in a table 'images' in mysql. There is another table 'restaurant' which needs to fetch those images and display respective images according to the restid. However, it is facing a problem in fetching the images and not displaying them. Please help!
This is imageupload.php:
<?php
require 'connect.inc.php';
?>
<html>
<head>
<title>Uploading image</title>
</head>
<body>
<?php
echo "<form action='imageupload.php' method='POST' enctype='multipart/form-data'>
Upload: <input type='file' name='image'><input type='submit' value='Upload' >
</form>";
if(isset($_FILES['image']['tmp_name']))
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image";
else
{
$query = "INSERT INTO images VALUES ('','$image_name','$image','22')";
$result = mysqli_query($con, $query);
if(!$result)
{
echo "Problem uploading";
}
else
{
echo "Image uploaded ";
$query2 = "SELECT * FROM images WHERE restid = '22'";
$result2 = mysqli_query($con,$query2);
while($info = mysqli_fetch_array($result2))
{
header("Content-type: image/jpeg");
echo $info['image'];
}
}
}
}
else
{
"Please upload a file";
}
?>
</body></html>
This is getimage.php (It fetches the image and displays it):
<?php
require 'connect.inc.php';
$id = $_REQUEST['id'];
$image = "SELECT * FROM images WHERE imgid = $id" ;
$image = mysqli_query($con, $image);
$image = mysqli_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
connect.inc.php is a file to connect to the database. I referred to other links but did not get any solid help. Please provide help.
Storing image in mysql should work.
Check that you don`t have any syntax errors.
Temporary remove Content-type header to see that image file gets printed (as gibberish string). Also check that mysql field you store image is BLOB type.
Post if you have any error there.

How to export image from MSSQL and display it on a website?

I'm having some difficulties getting this to work.
Here is the code I'm using:
require_once('db_connection.php');
$Query = mssql_query("SELECT Photo FROM Persons WHERE IDPerson = '3'");
$Person = mssql_fetch_array($Query);
$Photo = $Person["Photo"];
header('Content-type: image/jpeg');
echo $Photo;
This is the result I'm getting:
(There is something showing up on the bottom of the image, but that's
only part of the image)
This is how the image value looks like in my database:
0x424D3684030000000000360000002800000040010000F000000001001800000000000084030000000000000000000000000000000000FDFEF5FCFDF4FFFFFAE3F2DA
.......
Any ideas?
you should have to use use base64 encoding to display image from database
like
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />
use, base64 encoding
<?php
$Query = mssql_query("SELECT Photo FROM Persons WHERE IDPerson = '3'");
$Person = mssql_fetch_array($Query);
$Photo = $Person["Photo"];
$mime = null;
// place $type init. here
if ($type=="pjpeg") // <<< where do you get $type btw?
$mime = "image/jpeg";
$b64Src = "data:".$mime.";base64," . base64_encode($Photo);
echo '<img src="'.$b64Src.'" alt="" />';
?>
for example
header("Content-type: image/png");
echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLjZPLSxtRFIfVZRdWi0oFBf+BrhRx5dKVYKG4tLhRqlgXPmIVJQiC60JCCZYqFHQh7rrQlUK7aVUUfCBRG5RkJpNkkswrM5NEf73n6gxpHujAB/fOvefjnHM5VQCqCPa1MNoZnU/Qxqhx4woE7ZZlpXO53F0+n0c52Dl8Pt/nQkmhoJOCdUWBsvQJ2u4ODMOAwvapVAqSJHGJKIrw+/2uxAmuJgFdMDUVincSxvEBTNOEpmlIp9OIxWJckMlkoOs6AoHAg6RYYNs2kp4RqOvfuIACVFVFPB4vKYn3pFjAykDSOwVta52vqW6nlEQiwTMRBKGygIh9GEDCMwZH6EgoE+qHLMuVBdbfKwjv3yE6Ogjz/PQ/CZVDPSFRRYE4/RHy1y8wry8RGWGSqyC/nM1meX9IQpQV2JKIUH8vrEgYmeAFwuPDCHa9QehtD26HBhCZnYC8ucGzKSsIL8wgsjiH1PYPxL+vQvm5B/3sBMLyIm7GhhCe90BaWykV/Gp+VR9oqPVe9vfBTsruM1HtBKVPmFIUNusBrV3B4ev6bsbyXlPdkbr/u+StHUkxruBPY+0KY8f38oWX/byvNAdluHNLeOxDB+uyQQfPCWZ3NT69BYJWkjxjnB1o9Fv/ASQ5s+ABz8i2AAAAAElFTkSuQmCC');
if your string like this, it will print pdf logo

Save image as a blob type

I have used MySQL to save a image as a blob type. I 'm uploading files through PHP and when I get the image back I revive only a part of it. How can I improve the max size ? (my image file size is less than 300 KB)
PHP uploader...
if($_FILES!=null && $_POST!=null){
$file = $_FILES["image"]["tmp_name"];
if(!isset($file)){
echo "Please upload an image";
}else{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$type=$_POST['type'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image.";
else
{
if(!(mysql_query("INSERT INTO store (name,image,type) values ('$image_name','$image','$type')")))
echo "Problem uploading image";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded. <p /> Your image: <p /> <img id='imageId' src=get.php?id=$lastid>";
}
}
}
}
retrieving image
$id = addslashes($_REQUEST['id']) ;
$imageRow = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($imageRow);
$image = $image['image'];
header("Content-type: image/jpg");
echo $image;
You can use different types of blobs. Blob, Mediumblob, longblob, etc.
http://dev.mysql.com/doc/refman/5.0/en/blob.html
Use mysql_real_escape_string() to escape the image data, instead of addslashes(). addslashes() isn't meant for binary.
Use image like this:
<img src='file_display.php?id=<?php echo $row['id']; ?>' width='100' height='100'>
Here, $row['id'] is record primary key - id
and in file_display.php:
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
//connect to the db
$link = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
// select our database
mysql_select_db($database) or die(mysql_error());
// get the image from the db
$sql = "SELECT image FROM tbl_images WHERE id=" .$_GET['id'] . ";";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
// close the db link
mysql_close($link);
}
It works for me with WAMP 2.x package
you can use this following code ::
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$id = $_GET['id'];
$sql = mysql_query(" SELECT * FROM store WHERE id=$id") or die(mysql_error());
$row = mysql_fetch_array($sql);
header('Content: image/jpeg');
echo $row['image'];
?>

Categories