Jump to content

Save image to MySQL database


fcarentz

Recommended Posts

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

Archived

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

×
×
  • Create New...

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.