Page 1 of 1

uploading an image

Posted: Wed Sep 20, 2006 9:24 am
by aceconcepts
Hi, I have made a form that successfully passes all the variables required to upload an image file to a server.

The problem i now face is that no image file is actually uploaded!

I get the following two error messages:

Warning: move_uploaded_file(shop/product_images/image1.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/terrier/public_html/cms/shop/validate_product.php on line 22

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpNPqypi' to 'shop/product_images/image1.jpg' in /home/terrier/public_html/cms/shop/validate_product.php on line 22

I am stumped!

Below is the script used to upload the image file:

Code: Select all

<?php
//CONNECT TO SERVER AND DATABASE
$link=mysql_connect("localhost", "terrier_andrew", "b1scarr0sse")
	or die("Could not connect: " . mysql_error());
mysql_select_db("terrier_shopsmile32", $link)
	or die(mysql_error());

//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_unit_cost = $_POST['p_unit_cost'];
$prod_unit_price = $_POST['p_unit_price'];
$prod_stock_qty = $_POST['p_stock_qty'];


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

if (move_uploaded_file($_FILES['p_image']['tmp_name'], $ImageName)){
//if (copy($_FILES['p_image'] ['tmp_name'], $ImageName)){
	
	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="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',
			'$image_tempname',
			'$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);
		
		echo $newfilename . "added";
		}

		?>

Posted: Wed Sep 20, 2006 9:32 am
by ibbo
Its a directory permissions issue.

If you an chgrp apache (or your relavant web server user) then you will be able to get things working.

If not you can resort to ftp

Example

Code: Select all

class FTP{

	private $_host="127.0.0.1";
	private $_user="";
	private $_pass="";
	
	public $conn;
	public $login_result;
	
	public function __construct(){
		$this->conn = ftp_connect($this->_host); 
		$this->login_result = ftp_login($this->conn, $this->_user, $this->_pass);
		ftp_pasv($this->conn, true);
	}
	
	public function chdir($path){
		ftp_chdir($this->conn,$path);
	}
	
	public function save($from, $to){
		if(ftp_put ($this->conn, $to, $from, FTP_BINARY))
		  return true;
		else 
		  return false;
	}
	
	public function close(){
		ftp_close($this->conn);
	}
	
}


       if (move_uploaded_file($this->ImgFrom, $this->path.$this->ImgTo)) {
  		return true;
  	}else{
  		// Host dont let us chgrp so we use ftp as a work around
  		try{
  			
  			$ftp = new FTP();
  			$ftp->chdir($this->path);

  			if(!$ftp->save($this->ImgFrom, $this->ImgTo)){
  			  $ftp->close();	
  			  return false;

  			}else{
  			  $ftp->close();	
  			  return true;	

  			}

  		}catch(exception $e){
  			echo $e;
  		}
  	}
$this->path is the location you are saving to
$this->ImgFrom is your $_FILES['uploadimg']
$this->ImgTo is the filename you want to save it as.

move_upload_file is OK but some ISP's restrict your usage and prevent you using chmod and chgrp.

Solution FTP them instead.

ibbo

Posted: Wed Sep 20, 2006 9:33 am
by JayBird
Try using the FULL server path for $ImageName

Posted: Wed Sep 20, 2006 1:05 pm
by aceconcepts
Hi, Thanks for the response.

I am now able to upload the image file but it is uploading to the wrong directory.

I am getting confused now.

This is what i use to determine the desired directory:

Code: Select all

$ImageDir=$_SERVER['/public_html/shop/product_images/'];
However, the image is being uploaded elsewhere.

Any ideas?

Thanks

Posted: Thu Sep 21, 2006 3:53 am
by ibbo
Full paths dont start at /public_html.

More likely

/home/<user>/public_html/...

ibbo

Posted: Thu Sep 21, 2006 10:11 am
by pickle
Also, the temporary image name isn't stored in ['name'], but rather ['tmp_name']