Page 1 of 1

upload existing image file to mysql database

Posted: Wed Dec 30, 2009 9:51 pm
by paukacang
hello,
i'm new in php & mysql. my prob is, i don't know how to upload existing image file to mysql database.
here the example.

existing image
$file = "/picture/user.jpg";

want to insert to mysql database.
can anybody help plsssssssssss

Re: upload existing image file to mysql database

Posted: Wed Dec 30, 2009 10:22 pm
by rhecker
It is almost never a good idea to store images in the database. Never ever let the public do so. Better to store the images in a directory of the website and simply store the image name in the database. But since you asked, here is code that will accomplish what you THINK you want to do.


Code: Select all

 $uploadfile = $_FILES['upload']['tmp_name'];
  $inst_photoname = $_FILES['upload']['name'];
  $uploadtype = $_FILES['upload']['type'];
  if ($uploadfile != "") {
 $tempfile = fopen($uploadfile, 'rb');
 $photodata = fread($tempfile, filesize($uploadfile));
 $photodata = addslashes($photodata);
Then simply include $photodata in your INSERT statement.