can't upload images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

can't upload images

Post by aceconcepts »

Hi,

I am trying to upload images via a script i made. I get the following error:

Warning: move_uploaded_file(/home/chaplinf/public_html/shop/product_images/12sensodyne-baking-soda-dn.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/ace/public_html/cms/shop/validate_product.php on line 27

What does this mean and how can i resolve it?

Thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

here is the script i use to upload images:

Code: Select all

<?php
//MAKE VARIABLES AVAILABLE
$prod_code = $_POST['p_code'];
$prod_name= $_POST['p_name'];
$prod_description = $_POST['p_desc'];
$image_tempname = $_FILES['p_image'] ['name'];
$prod_type = $_POST['p_type'];
$prod_new_type=$_POST['p_new_type'];
$prod_unit_cost = $_POST['p_unit_cost'];
$prod_unit_price = $_POST['p_unit_price'];
$prod_stock_qty = $_POST['p_stock_qty'];

if(!empty($prod_new_type)){
	$prod_type=$prod_new_type;
	}

$img_name=$prod_code . ".jpg";

$ImageDir=$_SERVER['DOCUMENT_ROOT'] . "/shop/product_images/";
$ImageThumb=$ImageDir . "thumbs/";
$ImageName=$ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['p_image']['tmp_name'], $ImageName)){
	
	list($width, $height, $type, $attr) = getimagesize($ImageName);
	
	if ($type>3){
			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.";
	} else {
		//image is acceptable
		//*************************************
		//before adding product, check to see if it exists or if code is in use
		//******************************************************
		//insert into product table
		$insert="INSERT INTO product
					(product_id, prod_code, prod_name, prod_description, prod_image, prod_type, prod_unit_cost, prod_unit_price, prod_stock_qty)
			VALUES
			('$prod_code',
			'$prod_code',
			'$prod_name',
			'$prod_description',
			'$img_name',
			'$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 . $prod_code . ".jpg";
		//$newthumbname = $ImageThumb . $prod_code . ".jpg";
		if($type==2){
			rename($ImageName, $newfilename);
		} else {
			if($type==1){
			 $image_old=imagecreatefromgif($ImageName);
			} elseif ($type==3) {
			 $image_old=imagecreatefrompng($ImageName);
		
		//convert the image to jpg
		$image_jpg = imagecreatetruecolor($width, $height);
		imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);
		imagejpeg($image_jpg, $newfilename);
		imagedestroy($image_old);
		imagedestroy($image_jpg);
		}
		}
		
		$newthumbname = $ImageThumb . $prod_code . ".jpg";
	

		//get the dimensions for the thumbnail
		$thumb_width = $width * 0.40;
		$thumb_height = $height * 0.40;
		
		//create the thumbnail

		$largeimage = imagecreatefromjpeg($newfilename);
	
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		
		imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
		imagejpeg($thumb, $newthumbname);
		imagedestroy($largeimage);
		imagedestroy($thumb);
		
		
	}
}
	?>

<html>
<head><link rel="stylesheet" href="../../css/verdana_body.css"></head>
<table cellpadding="0" cellspacing="0" width="740">
<tr><td colspan="2" height="30" valign="top"><b>Product added: <? echo $prod_name . " [" . $prod_code . "]"; ?></b></td></tr>
<tr><td height="30" width="114">Code:</td><td width="624"><? echo $prod_code; ?></td></tr>
<tr><td height="30">Name:</td><td><? echo $prod_name; ?></td></tr>
<tr><td height="30">Description:</td><td><? echo $prod_description; ?></td></tr>
<tr><td height="30">Image file:</td><td><a href="http://www.smile32.co.uk/shop/product_images/<? echo $prod_code . ".jpg"; ?>" target="_blank"><? echo $prod_code . ".jpg"; ?></a></td></tr>
<tr><td height="30">Type/Category:</td><td><? echo $prod_type; ?></td></tr>
<tr><td height="30">Unit Cost:</td><td><? echo $prod_unit_cost; ?></td></tr>
<tr><td height="30">Unit Price:</td><td><? echo $prod_unit_price; ?></td></tr>
<tr><td height="30">Stock Qty:</td><td><? echo $prod_stock_qty; ?></td></tr>
<tr><td height="50" colspan="2"><a href="http://www.smile32.co.uk/cms/shop/new_product.php">Add another product</a></td></tr>
</table>
</html>
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

does "/home/chaplinf/public_html/shop/product_images/12sensodyne-baking-soda-dn.jpg" exist?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

the directory exists but the image file doesn't bcos it doesn't get uploaded.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I've resolved it.

It was my own stupid fault. I was using a sub domain as opposed to www and I was replacing the incorrect file because i was in the wrong dir.

Oh well, thanks a lot for your comment.

Regards.
Post Reply