Author Topic: Upload picture to database  (Read 145 times)

0 Members and 1 Guest are viewing this topic.

Offline outatimeTopic starter

  • Irregular
    • View Profile
Upload picture to database
« on: March 14, 2010, 10:22:54 PM »
phpMyAdmin 2.6.3-pl1

I'm trying to upload a picture to a database. The part I'm really confused about is if the database will actually hold the file or do I need to upload it to the server then somehow put the link in the database. Here is some of the code that I found to use. And how much of this code do I actually need? Thanks



Code: [Select]
if ($act=='view'){
$sql="SELECT * FROM tbl_image where id=$id";
$rst=mysql_query($sql) or die('gagal');
$data=mysql_fetch_array($rst);
$type=$data[type];
Header("Content-type: $type");
echo $data[image];
}

if ($act=='del'){
$sql="DELETE FROM tbl_image where id=$id";
$rst=mysql_query($sql) or die('gagal');
}

if($submit)
{
$type = $_FILES['file']['type'];
copy ($_FILES['file']['tmp_name'], "tmp/tmp.jpg") or die ("Could not copy");
$filer="tmp/tmp.jpg";
$handle = fopen($filer, "r");
$pure = addslashes(fread($handle, filesize($filer)));
$sql = "insert into tbl_image(image,type) values('$pure','$type')";
$result = mysql_query($sql,$cn)or die(mysql_error());
}

$sql="SELECT * FROM tbl_image";
$rst=mysql_query($sql) or die('gagal');
while ($data=mysql_fetch_array($rst)){
$id=$data[id];
echo "picture no $id <a href='./image.php?act=del&id=$id'>delete</a><br><img src='./image.php?act=view&id=$id'><br><br>";
}

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: Upload picture to database
« Reply #1 on: March 19, 2010, 08:42:06 AM »
If you "found" code, then ask whomever "wrote" it.

FYI, you can choose to do either.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.