Jump to content


Photo

Save image to MySQL database


  • Please log in to reply
1 reply to this topic

#1 fcarentz

fcarentz
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 March 2006 - 08:28 PM

Ok so I need to upload an image from a form to be stored inside the database as a Blob type object. Can anyone provide some insight I just can't seem to get the dang thing to work correctly.

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 01:29 AM

on the html form end, you just need to specify the form enctype as multipart/form-data

<form enctype="multipart/form-data" method="post">
<input name="userfile" type="userfile">
</form>

On the PHP end, this should work:

mysql_query("INSERT INTO `images` (`name`,`content`)\nVALUES ('".$_FILES[key($_FILES)]['name']."','".mysql_real_escape_string(file_get_contents($_FILES[key($_FILES)]['tmp_name']))."')");

of course, I'm just posting the filename (field name `name`) and image "content" (field name `content`) to the table `images`. Your names will be different.

Also, using $_FILES[key($_FILES)]['name'] instead of $_FILES['userfile']['name'] would allow you to include the mysql_query in a while(next($_FILES)) loop to upload multiple files (provided your form has multiple file <input>s). But it works even if you only upload one file (and doesn't care what you name the <input>.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users