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

<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

Recommended Answers

All 14 Replies

Member Avatar for diafol

1. Don't submit a form (upload a file) to the same page.
2. Cut out all the extraneous html, it just gets in the way.


Your form:

<form action="addproduct.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image" />
<input type="submit" name="submit" value="Add">
</form>

You send to addproduct.php, but you should send to a formhandler page.

Your php (something like this, not tested):

<?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.");
    $lastid = mysql_insert_id();
    echo "Image uploaded. <br />Your image:<p /><img src=get.php?id=$lastid>";
  }
} 

?>

thanx for the reply. i have done what you have said but i'm still getting the same promblem.

i now have 3 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.");
  $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;
?>

hi there i really appreciate you helping me, it now adds to the database but the image does not display once added. I was wondering if you knewwhat the problem was?? (my code is the same as above)

You could try this:

<?php

if ( isset( $_POST['submit'] ) ) { //Check if form was submitted
	$name = 'image';
	$allowed_extns = array('jpg','jpeg','gif','png');
	if ( !isset( $_FILES[$name] ) || $_FILES[$name]['error'] !== UPLOAD_ERR_OK || empty( $_FILES[$name]['name'] ) ) {
		echo 'Please choose a file to upload'; //File was not uploaded
	}
	elseif ( !is_uploaded_file( $_FILES[$name]['tmp_name'] ) ) {
		//not a valid file
	}
	else {
		$extn = explode( '.',$_FILES[$name]['name'] );
		$extn = strtolower( end( $extn ) );
		if ( !in_array( $extn,$allowed_extns ) ) {
			//file extension not allowed
		}
		elseif ( !getimagesize( $_FILES[$name]['tmp_name'] ) ) {
			//not an image
		}
		else {
			echo 'ok';
			//insert into database here
		}
	}
}

?>
<form action="addproduct.php" method="post" enctype="multipart/form-data">
	Image: <input type="file" name="image" id="image" /><br />
	<input type="submit" name="submit" value="Add" />
</form>

I haven't tested it, but it works in my head.

thank you for your reply kkeith29 :) i tried the code you gave me but it doesn't work :(

Member Avatar for diafol

OK:
....handler page....

<?php
include('config.php');

if (is_uploaded_file($_FILES['image']['tmp_name'])){
   	$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){
	  	echo "Thats not an image";
	}else{
	  	$insert = mysql_query("INSERT INTO images VALUES ('','$image_name','$image')") or die("Problem uploading image.");
  		$lastid = mysql_insert_id();
	  	echo "Image uploaded. <p>Your image:</p><a href=\"get.php?id=$lastid\">Show Image</a>"; 
   	}
} else {
   echo "Weirdness";
}
?>

....form page....

<form enctype="multipart/form-data" method="post">
	<input type="file" name="image" id="image" />
	<input type="submit" id="submit" name="submit" value="Test this" />
</form>

....get page....

include('config.php') or die(mysql_error());
 
$id = addslashes($_GET['id']);
 
$q = mysql_query("SELECT * FROM images WHERE id=$id");
$r = mysql_fetch_assoc ($q) ;
$img = $r['image'];
 
header ("Content-type: image/jpeg");
 
echo $img;

If you want to show the image instead of the 'Show Image' text, I suggest using the get code as a function:

function getImage($id){
  ...
}

I just tested the code on my local server and the image uploads fine.

Questions:
1. Is a record getting inserted into the database?
2. If so, are you only uploading jpeg images? The header you are outputting on the display page is only for jpegs.

There are some other obvious things, I hope you changed in my code before you tried it. I won't ask about them.

thanks for the reply ardav ive tried it but no success it won't add to the database :(
how could i use the get image function?? sorry i don't know much in php. BTW is your image real? lol. It looks well nasty lol!!

hi there kkeith29 thanks for helping :) i tried the code but it doesn't add to the database :( the following code does add to it but it does not display after it is added

//addproduct.php
<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>
//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 ('NULL','$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;
?>
Member Avatar for diafol

Strangely enough adams, this is one piece of code I did try - it worked for me.


And no, the image isn't me, it's one of a former Welsh rugby player called Garin Jenkins. The guy was sunk to the second knuckle in his eyeball. The guy was lucky Garin couldn't see him to 'have a word in his ear'. :)

hmmm i must be doing somthing wrong that i'm unaware of.

first i have my form page

//addproduct.php
 
      <form action="addproduct_action.php" enctype="multipart/form-data" method="post">
       
       <input type="file" name="image" id="image" />

      <input type="submit" id="submit" name="submit" value="Test this" />

      </form>

Then i have my action page

//addproduct_action.php
<?php
include('config.php');

if (is_uploaded_file($_FILES['image']['tmp_name'])){
   	$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){
	  	echo "Thats not an image";
	}else{
	  	$insert = mysql_query("INSERT INTO images VALUES ('','$image_name','$image')") or die("Problem uploading image.");
  		$lastid = mysql_insert_id();
	  	echo "Image uploaded. <p>Your image:</p><a href=\"get.php?id=$lastid\">Show Image</a>"; 
   	}
} else {
   echo "Weirdness";
}
?>

finally i have my get page

//get.php
<?php
include('config.php') or die(mysql_error());
 
$id = addslashes($_GET['id']);
 
$q = mysql_query("SELECT * FROM images WHERE id=$id");
$r = mysql_fetch_assoc ($q) ;
$img = $r['image'];
 
header ("Content-type: image/jpeg");
 
echo $img;
?>

In the database i have
id int(5) auto_increment
name varchar(100)
image blob

I was wondering if you could spot where i'm going wrong?

Wow his finger went in that far? He got fingered haha!! Surely that guy must have lost his sight? ooo that's gotta hurt

Add single quotes '' around your src link in the image tags. <img src='get.php?id=$lastid' /> Then it should at least upload to you mysql. They are missing in your code. Hope it helps. ;)

even if i add the single quotes don't work....
pleaze helps

I think this link is help to u Click Here image are stored in database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.