display image from mysql(blob data) | Newbie | | Join Date: Dec 2007
Posts: 11
| |
HI all,
I am able to upload the image as blob to mysql. but while displaying the image i cant display it properly .
The code used for uploading image to mysql inserts data into mysql table.The uploading code is: -
MYSQL_CONNECT("localhost","root","");
-
mysql_select_db("sample");
-
-
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
-
-
$result=MYSQL_QUERY("INSERT INTO PHOTO(description,bin_data,filename,filesize,filetype) ".
-
"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
-
The code used for displaying the image is as follows -
<?php
-
MYSQL_CONNECT("localhost","root","");
-
mysql_select_db("sample");
-
-
$id=2;
-
-
$query = "select filename,filetype,filesize,bin_data from PHOTO where id=$id";
-
-
$result = mysql_query($query) or die('Error, query failed');
-
//$ROW = mysql_fetch_assocc($result);
-
list($name, $type, $size, $content) = mysql_fetch_array($result);
-
-
echo '<meta http-equiv="Content-Type" content="img/jpg" />';
-
echo $content;
-
exit;
-
-
?>
-
This code displays some random characters all over the browser but not the image. I think i have been missing some statements .
Thank you
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: display image from mysql(blob data)
You have to set the headers properly.
Read this tutorial. (scroll down to the part about downloading.)
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: display image from mysql(blob data)
this might help u
[PHP]$query = "SELECT Doc,Doc1,Doc2,Name,Name1,Name2 FROM xyz WHERE id = '".$id."'";
$result = mysql_query($query) or die('Error, query failed');
list($Doc,$Doc1,$Doc2,$Name,$Name1,$Name2) = mysql_fetch_array($result);
//header("Content-length: $size");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name");
echo $Name;
echo $Doc;
[/PHP]
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: display image from mysql(blob data) Quote:
Originally Posted by pradeepjain this might help u
[PHP]$query = "SELECT Doc,Doc1,Doc2,Name,Name1,Name2 FROM xyz WHERE id = '".$id."'";
$result = mysql_query($query) or die('Error, query failed');
list($Doc,$Doc1,$Doc2,$Name,$Name1,$Name2) = mysql_fetch_array($result);
//header("Content-length: $size");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1 changed on 27th july commented this and next line and addedprevious 3 lines for IE caching prob
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$Name");
echo $Name;
echo $Doc;
[/PHP] He's not trying to download a pdf.
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: display image from mysql(blob data)
Hey really srry ...i did not notice tht,,this is wht wll work for him
[PHP]
error_reporting(E_ALL);
$link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("xyz") or die(mysql_error());
$sql = "SELECT Pic FROM abc WHERE ID='".$_GET['Id']."'";
//'".$_GET['Id']."'";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
header("Content-type: image/jpeg");
$test=mysql_result($result,0);
$desired_width = 85;
$desired_height = 110;
$im = imagecreatefromstring($test);
$new = imagecreatetruecolor($desired_width, $desired_height);
$x = imagesx($im);
$y = imagesy($im);
imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);
imagedestroy($im);
header("cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-type: image/jpeg');
imagejpeg($new, NULL, 85);
imagedestroy($new);
// echo $test;
mysql_close($link);
[/PHP]
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,383 network members.
|