Jump to content


This topic is now archived and is closed to further replies.


Save image to MySQL database

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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">

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>.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.