471,887 Members | 710 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how to store image in Mysql database

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
8 12009
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
3,237 Expert 2GB
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
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
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
3,237 Expert 2GB
{

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
Here i tried...
print $log['file'];
but the result came as OX
plz give me an solution
Feb 9 '07 #7
Nert
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
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.

Similar topics

reply views Thread by zermasroor | last post: by

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.