By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

how to store image in Mysql database

P: 34
hi
i need help..how can i store image in mysql database
and i want to retrive that image
can anyone suggest plz
Feb 8 '07 #1
Share this Question
Share on Google+
8 Replies


stephane
P: 35
there is BLOB datatype in Mysql

to put it in, read image file with file_get_contents() function and put content into mysql query
Feb 8 '07 #2

Motoma
Expert 2.5K+
P: 3,235
there is BLOB datatype in Mysql

to put it in, read image file with file_get_contents() function and put content into mysql query
Thank you for your response stephane!
Feb 8 '07 #3

P: 34
Expand|Select|Wrap|Line Numbers
  1. <form name="form1" enctype="multipart/form-data" method="post" action="">
  2.   <input name="file1" type="file" id="file1">
  3.   <input type="submit" name="Submit" value="Submit">
  4. </form>
  5. <?
  6. $f=$_POST['file1'];
  7. mysql_connect("localhost","root","");
  8. mysql_select_db("real");
  9. mysql_query("select * from reg");
  10. //$file_u=$_POST['file1'];
  11.  
  12.  
  13.  
  14.                 $val = '0x' . bin2hex($f);
  15.                 $qu="insert into `reg`(`file`)values('$val')";
  16.  
  17. mysql_query($qu) or die("Query Error");
  18. print '<h1 align="center">File Successfully Uploaded</h1>';
  19. ?>
  20.  

Here i inserted the image in database.its stored as BLOB 2BYTES
and i tried to retrived by using this code
Expand|Select|Wrap|Line Numbers
  1. <?
  2. mysql_connect("localhost","root","");
  3. mysql_select_db("real");
  4. $q=mysql_query("select * from reg");
  5. while($log=mysql_fetch_array($q))
  6. {
  7.  
  8. print file_get_contents($log['file']);
  9. }
  10. ?>
  11.  
i dint get the image.....
Feb 8 '07 #4

stephane
P: 35
while($log=mysql_fetch_array($q))
{
print file_get_contents($log['file']);
}

.
why u use file_get_contents here?
i think it must looks like this
Expand|Select|Wrap|Line Numbers
  1. while($log=mysql_fetch_array($q))
  2. {
  3. print $log['file'];
  4. }
  5.  
and don't foget to send image/jpg ( or other ) before
Feb 8 '07 #5

Motoma
Expert 2.5K+
P: 3,235
{

print file_get_contents($log['file']);
}
This is wrong, first, you need to specify the content type: jpeg, gif, png, etc...
Second, you aren't opening a file, so file_get_contents() should not be used.

You will want to do something more like this:
Expand|Select|Wrap|Line Numbers
  1. {
  2. header("Content-type: image/png");
  3. echo $log['file'];
  4. }
  5.  
Hope this helps.
Feb 8 '07 #6

P: 34
Here i tried...
print $log['file'];
but the result came as OX
plz give me an solution
Feb 9 '07 #7

Nert
P: 64
Here i tried...
print $log['file'];
but the result came as OX
plz give me an solution
Hi hemashiki,

according to you, you have already save image to database right? now you want to retrieve the image?
here try this code, i've come up with this code when i encountered the same problem as yours.

Expand|Select|Wrap|Line Numbers
  1. mysql_connect("localhost","root","");
  2. mysql_select_db("real");
  3. $q=mysql_query("select * from reg") or die(mysql_error());
  4. $log = mysql_fetch_object($q); 
  5. header("Content-type: image/GIF");    
  6. echo $log->file; 
hope this would help you too.

--nert (^_^)
Feb 9 '07 #8

stephane
P: 35
Here i tried...
print $log['file'];
but the result came as OX
plz give me an solution
because you insert to base at the same hex format
$val = '0x' . bin2hex($f);
it's wrong, you must insert simply file content, returned file_get_contents function
Feb 9 '07 #9

Post your reply

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