PDA

View Full Version : my image upload php script won't work plz help



adams2887
03 Apr 2011, 01:02 PM
hi i am trying to upload an image but it doesn't work:( I have been trying for ages but without any luck. I was wondering if anyone would be kindly enough to take a look at it.

Basicly i'm getting my echo error message "problem uploading image" displayed constantly, even before i try upload it keeps saying it. It is also not uploading.
my database
id (int5) auto_increment
image_name (varchar100)
image (blob)

Here is my code i have two php files.
//addproduct.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="600" height="382" border="3" BORDERCOLOR="#CD3788" bgcolor="#FFFFFF" align="center" cellpadding="0" cellspacing="2">
<tr>
<form action="addproduct.php" method="post" enctype="multipart/form-data">
<td>
<table width="100%" border="0" BORDERCOLOR="#CD3788" cellpadding="3" cellspacing="1">
<tr>
<td colspan="3"><strong>Add Product</strong></td>
</tr>

<tr>
<td><font color="black">Add Image</font></td>
<td>:</td>
<td><input type="file" name="image" id="image" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Add">
</td>
</tr>
</table>
</td>

</form>
</tr>
</table>

<?php
// connect to host and database
include('config.php') or die(mysql_error());

// file properties
$file = $_FILES['image']['tmp_name'];

if (!isset ($file))
echo "Please select an image";
else
{
$image = addslashes (file_get_contents ($_FILES['image'] ['tmp_name']));
$image_name = addslashes ($_FILES['image'] ['image_name']);
$image_size = getimagesize ($_FILES['image'] ['tmp_name']);

if ($image_size==false)
echo "Thats not an image";
else
{
if (!$insert = mysql_query ("INSERT INTO products VALUES ('','$image_name','$image')"))
echo "Problem uploading image.";
else
{
$lastid = mysql_insert_id ();
echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";
}
}

}
?>
</body>
</html>

//get.php

<?php
include('config.php') or die(mysql_error());

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM products WHERE id=$id");
$image = mysql_fetch_assoc ($image) ;
$image = $image ['image'];

header ("Content-type: image/jpeg");

echo $image;
?>

please help.
thankyou for your time :)