Page 1 of 1

image upload

Posted: Tue Mar 14, 2006 7:38 pm
by aceconcepts
Hi,

I am having difficulties trying to upload an image file.

I am using an html form as data entry for selecting the image.

Below is the php code i am using to upload the selected image file to my database:

Code: Select all

<?php
//CONNECT TO SERVER AND DATABASE
include "conn.inc.php";

//MAKE VARIABLES AVAILABLE
$prod_code = $_POST['prod_code'];
$prod_name= $_POST['prod_name'];
$prod_description = $_POST['prod_description'];
$prod_image = $_FILES['prod_image'] ['name'];
$prod_type = $_POST['prod_type'];
$prod_unit_cost = $_POST['prod_unit_cost'];
$prod_unit_price = $_POST['prod_unit_price'];
$prod_stock_qty = $_POST['prod_stock_qty'];

//UPLOAD PRODUCT AND CHECK FOR IMAGE TYPE
//CHANGE IMAGE PATH TO MATCH DIR
$ImageDir = "c:/Program Files/Apache Group/Apache2/test/ace_cart/admin/images/";
$ImageName = $ImageDir . $prod_image;

if (move_uploaded_file($_FILES['prod_image'] ['tmp_name'], 
										$ImageName)) {

//GET INFO ABOUT THE IMAGE BEING UPLOADED
list ($width, $height, $type, $attr) = getimagesize($ImageName);

switch ($type) {
	case 1:
		$ext = ".gif";
		break;
	case 2:
		$ext = ".jpg";
		break;
	case 3:
		$ext = ".png";
		break;
	default:
		echo "Sorry, but the file you uploaded was not a gif, jpg or png file.<br>";
		echo "Please hit your browser's 'back' button and try again.";
	}
	
//INSERT PRODUCT INTO PRODUCT TABLE
$insert = "INSERT INTO product
			(prod_code, prod_name, prod_description, prod_image, prod_type, prod_unit_cost, prod_unit_price, prod_stock_qty)
			VALUES
			('$prod_code',
			'$prod_name',
			'$prod_description',
			'$prod_image',
			'$prod_type',
			'$prod_unit_cost',
			'$prod_unit_price',
			'$prod_stock_qty')";
$insertresults = mysql_query($insert)
	or die(mysql_error());
}

$lastpicid = mysql_insert_id();

$newfilename = $ImageDir . $lastpicid . $ext;

rename($ImageName, $newfilename);

?>
The above code results in the following error:

Warning: rename(c:/Program Files/Apache Group/Apache2/test/ace_cart/admin/images/,c:/Program Files/Apache Group/Apache2/test/ace_cart/admin/images/0) [function.rename]: Permission denied in C:\Program Files\Apache Group\Apache2\test\ace_cart\admin\check_product.php on line 61

I tried to just echo the image file at line 14 and nothing was echoed!!!

Any help would be much appreciated,

Nick

Posted: Tue Mar 14, 2006 7:39 pm
by nickman013
$ImageDir = "c:/Program Files/Apache Group/Apache2/test/ace_cart/admin/images/";
I dont know if you can use this as a directory.

Posted: Tue Mar 14, 2006 7:44 pm
by feyd
are you sure something uploaded? post the <form> tag you are using against this script.

Posted: Tue Mar 14, 2006 7:52 pm
by aceconcepts
Here is the form tag:

Code: Select all

<form name="form1" method="post" action="check_product.php">
  <table width="760" border="0" cellspacing="0" cellpadding="0">
    <tr> 
      <td width="142" height="30">Code</td>
      <td width="618" height="30"> <input name="prod_code" type="text" id="prod_code"></td>
    </tr>
    <tr> 
      <td height="30">Name</td>
      <td height="30"> <input name="prod_name" type="text" id="prod_name"></td>
    </tr>
    <tr> 
      <td height="30">Description</td>
      <td height="30"> <textarea name="prod_description" id="prod_description"></textarea></td>
    </tr>
    <tr> 
      <td height="30">Image</td>
      <td height="30"><input name="prod_image" type="file" id="prod_image"></td>
    </tr>
    <tr> 
      <td height="30">Type</td>
      <td height="30"> <input name="prod_type" type="text" id="prod_type"></td>
    </tr>
    <tr> 
      <td height="30">Unit Cost</td>
      <td height="30"> <input name="prod_unit_cost" type="text" id="prod_unit_cost"></td>
    </tr>
    <tr> 
      <td height="30">Unit Price</td>
      <td height="30"> <input name="prod_unit_price" type="text" id="prod_unit_price"></td>
    </tr>
    <tr> 
      <td height="30">Stock Qty</td>
      <td height="30"> <input name="prod_stock_qty" type="text" id="prod_stock_qty"></td>
    </tr>
    <tr> 
      <td height="30">&nbsp;</td>
      <td height="30"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>
I can't even echo the file path once the form has been submitted.

Posted: Tue Mar 14, 2006 8:04 pm
by aceconcepts
Found it!

I left out:

enctype="multipart/form-data

from the form definition.

It works now.

Thanks for your prompt replies.