upload existing image file to mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paukacang
Forum Newbie
Posts: 1
Joined: Wed Dec 30, 2009 9:48 pm

upload existing image file to mysql database

Post 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
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: upload existing image file to mysql database

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