473,421 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

display image from mysql(blob data)

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:
Expand|Select|Wrap|Line Numbers
  1. MYSQL_CONNECT("localhost","root","");
  2.     mysql_select_db("sample");
  3.  
  4.     $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
  5.  
  6.     $result=MYSQL_QUERY("INSERT INTO PHOTO(description,bin_data,filename,filesize,filetype) ".
  7.         "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
  8.  
The code used for displaying the image is as follows
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     MYSQL_CONNECT("localhost","root","");
  3.     mysql_select_db("sample");
  4.  
  5. $id=2;
  6.  
  7.  $query = "select filename,filetype,filesize,bin_data from PHOTO where id=$id";
  8.  
  9. $result = mysql_query($query) or die('Error, query failed');
  10. //$ROW = mysql_fetch_assocc($result);
  11. list($name, $type, $size, $content) = mysql_fetch_array($result);
  12.  
  13. echo '<meta http-equiv="Content-Type" content="img/jpg" />';
  14. echo $content;
  15. exit;
  16.  
  17. ?>
  18.  
This code displays some random characters all over the browser but not the image. I think i have been missing some statements .
Thank you
Jul 4 '08 #1
4 8124
Markus
6,050 Expert 4TB
You have to set the headers properly.

Read this tutorial. (scroll down to the part about downloading.)
Jul 4 '08 #2
pradeepjain
563 512MB
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]
Jul 4 '08 #3
Markus
6,050 Expert 4TB
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.
Jul 4 '08 #4
pradeepjain
563 512MB
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]
Jul 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: owardman | last post by:
Dear All. Informix 9.3 Solaris 9 MySQL 4.1.10 Linux I am trying to migrate blob data from Informix to MySQL. Has anybody managed this successfully and if so how? Thanks in advance
2
by: RedSouljaz | last post by:
Hi, How to display image that was saved in database ms sql server 2000 into picture box. The field type that I use in database is Images I can save to database but cannot show from database. ...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
1
by: sang | last post by:
Hi i am doing my project in Mysql and PHP. My problem is i want to send a Blob field to my email using the Php. That is i am storing Candidates Resume in Blob field(Document format). I want to...
1
by: jamal8t2 | last post by:
i have inserted blob data in database , but i am not able to access back that data to the browser. i have php script but it does not work proprly. our script is below if ($_REQUEST == 1) { ...
5
by: faizalahmd | last post by:
How to display image in image control , which is retrieved from the data base . using c# in asp.net regards, FAIZAL AHMED.H
1
by: waleedmazhar | last post by:
Hello Everybody Can anybody tell me when to save images in mysql blob and when save image folder and save the image name in database. which is more better saving image into database or save it...
6
by: phpmagesh | last post by:
Hi I have tried this sample code in my database for file update, but some error occurred. everything is going fine. uploaded the file successfully but, when i download the file which i...
0
by: Abhilash Etikala | last post by:
I am having a problem in displaying the image. Image is stored perfectly but not able to retrieve and display...i tried to knowledge but still not able to get it.. my code is: display.php ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.