Forum Moderators: coopster
If someone has uploaded a file, you will find it here:
$_FILES[fieldname][tmp_name] where fieldname is the name="" parameter of your <INPUT TYPE="FILE" element in your form. You then read that file into memory:
$filecontents=file_get_contents($_FILES[fieldname][tmp_name]); Insert it into MySQL by escaping it and putting it into a BLOB field:
mysql_query("INSERT INTO `images` (`imagename`,`imagecontents`) VALUES ('".mysql_escape_string($_FILES[fieldname][name])."','".mysql_escape_string($filecontents)."')"); When you eventually want to display it again, you'll need to get it from the database:
$r=mysql_fetch_assoc(mysql_query("SELECT * FROM `images` WHERE `imagename` = '$theimagename'")); header("Content-type: image/jpeg"); header("Content-length: ".len($r[imagecontents])); print $r[imagecontents]; If you want to dynamically do things with the image using GD... then create the GD image and store it in GD's own format - much faster for loading and quickly thumbnailing, cropping, whatever on-the-fly
First insert the record into a table which uses auto_increment on one field...
then:
file_put_contents("files/".mysql_insert_id(),file_get_contents($_FILES[fieldname][tmp_name])); When it comes to recalling it... if the auto_increment field is 'imageid' then just:
$image=file_get_contents("files/".$r[imageid]);
And proceed as before.
I would also suggest to store images outside the database. Then you'll be able to link them directly from web page instead of loading from db each time. Will work faster.