uploading an image

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

uploading an image

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

		?>
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post 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
Last edited by ibbo on Wed Sep 20, 2006 9:52 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Try using the FULL server path for $ImageName
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post by ibbo »

Full paths dont start at /public_html.

More likely

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

ibbo
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Also, the temporary image name isn't stored in ['name'], but rather ['tmp_name']
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply