fcarentz Posted March 3, 2006 Share Posted March 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4035-save-image-to-mysql-database/ Share on other sites More sharing options...
michaellunsford Posted March 4, 2006 Share Posted March 4, 2006 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>. Quote Link to comment https://forums.phpfreaks.com/topic/4035-save-image-to-mysql-database/#findComment-14049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.