I have products that I load to the DB, but now I need to upload an image with each product. I have no problem uploading the imagefile to specified folder on the server, but how does my product know which image to use?
Obviously I need a path to image field in the database.
This is what I have tried so far, and I know it's not right, because it's not working.
<?
if(isset($_POST['upload']))
{
mysql_connect("localhost", "username", "password");
mysql_select_db("DataBase");
$file = fopen($_FILES['imagefile']['tmp_name'], "r");
$image = addslashes(fread($file, filesize($_FILES['imagefile']['tmp_name'])));
mysql_query("insert into images values('null', '$image')") or die(mysql_error());
echo "image uploaded successfully";
copy ($_FILES['imagefile']['tmp_name'], "images/".$_FILES['imagefile']['name'])
or die ("Could not copy");
echo "";
echo "<br>";
echo "Name: ".$_FILES['imagefile']['name']."";
echo "<br>";
echo "Size : ".$_FILES['imagefile']['size']."";
echo "<br>";
echo "Type : ".$_FILES['imagefile']['type']."";
echo " Copy Done....";
}
?>
<form action="" enctype="multipart/form-data" method="post" name"">
<input type="file" name="image" /><br />
<input type="submit" name="upload" value="Upload" />
</form>
Is there no easier way of doing this?