My image upload php script won't work plz help

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. I will attcah them also to make it easier to look at


//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 :slight_smile:

the error is at the database, however you will need to move the file to a directory you want (move_uploaded_file) as I can’t see this in the code.
For the error, after this line: echo “Problem uploading image.”;
add this: echo mysql_error();
so you can see what is the error.

thanks for the reply i deleted the echo “problem loading image”;
and replaced it with
echo mysql_error();
i am now getting an error on my page

Access denied for user ‘myuser’@‘srv207.one.com’ (using password: NO)

I was wondering if you knew how to solve it?

Well… it seems that you have to go to config.php and set the correct user and password for the database you
are using. Do you have one for it?

yes, ive been using it for the users to register and it works strange :s lol. i have managed to edit the code so the form goes to an action page instead of the php being on the same page. I have managed to get it to successfully add to the database however it will not display here is my 3 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_action.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>
</body>
</html> 


//addproduct_action.php
<?php
include('config.php');
$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']['name']);
  $image_size = getimagesize($_FILES['image']['tmp_name']);
} 
if($image_size==false){
  echo "Thats not an image";
}else{
  
  $insert = mysql_query("INSERT INTO products VALUES ('','$image_name','$image')") or die("Problem uploading image.");
  echo mysql_error();
  $lastid = mysql_insert_id();
  echo "Image uploaded. <br />Your image:<p /><img src=get.php?id=$lastid>";
}
?>


//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;
?>

I usually keep the images in the filesystem and store a reference to it in the database thus keeping the db small in size…
Are you sure you are getting a unique id with the query, using an empty string? Try changing it to this:
INSERT INTO products VALUES (NULL,‘$image_name’,‘$image’)

thanks for replying steve. I gave that a go but it’s still not displaying the image

Hi

It happen that I ofter use the following script, so this solution is tested & debbuged. Check if you can use it. The difference is that I store my files in folder, and only the file name is stored in the database:

Here is the code:



$image_id=mysql_real_escape_string($_POST['image_id']);
    $artid=mysql_real_escape_string($_POST['artid']);
    $sorder=mysql_real_escape_string($_POST['sorder']);
    $visible=mysql_real_escape_string($_POST['visible']);
    $filename =mysql_real_escape_string($_FILES['filename']['name']);

    // find size
    if ($_FILES['filename']['size']>204800) {
        //go to error page
        Header ("Location: errorupload.php?size=over");
    } else {
        //else go to ok page
        Header ("Location: articleimages.php");
    }

    // find file extension
    $valid_extensions = array('gif', 'jpg', 'jpeg', 'png', '');
    if (!in_array(end(explode('.', strtolower($_FILES['filename']['name']))), $valid_extensions)) {
        Header ("Location: errorupload.php?type=wrong");
    } else {
        Header ("Location: articleimages.php");
    }

    // rename the file(s)
    if ($filename !='') {
        //remove the eventual space
        $filename = str_replace(' ', '_',$filename);
        //get the timestamp
        $now = date('Y-m-d-His');
        //add the timestamp to the name
        $filename = $now.$filename;
    }

    //upload the files
    $folder = "myFolder/";
    move_uploaded_file($_FILES['filename']['tmp_name'], $folder.$filename);

    if (empty($filename)) {
        $query = "UPDATE articleimages SET artid ='$artid', visible='$visible', sorder='$sorder'
                                       WHERE image_id = '$image_id'";
    } else {
        $query = "UPDATE articleimages SET artid ='$artid', filename ='$filename', visible='$visible',
                                           sorder='$sorder'
                                       WHERE image_id = '$image_id'";
    }
    mysql_query($query);
    mysql_close();


This is the part, where I actually update my table, so to perofrm the insert, you will need to change the script. myFolder will need 777 permissions so the image can be uploaded. Otherwise, you will have the record in the database, but the image will not be uploaded.

I hope that this may help you.

Regards, Zoreli