0

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

6
Contributors
14
Replies
30
Views
6 Years
Discussion Span
Last Post by Raju_3
0

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>";
  }
} 

?>

Edited by diafol: bstard monkey hands

0

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

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)

0

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.

0

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

0

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){
  ...
}

Edited by diafol: n/a

0

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.

0

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!!

0

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

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'. :)

Edited by diafol: n/a

Attachments Capture.PNG 52.47 KB
0

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

0

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. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.