Page 1 of 1

image upload

Posted: Mon Mar 27, 2006 7:18 pm
by aceconcepts
Hi,

I get this message:

Warning: move_uploaded_file(/images/animal_party_elephant_mask.gif): failed to open stream: No such file or directory in /home/ace/public_html/ace_cart/admin/check_product.php on line 20

when i run this script:

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 = "/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
			(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',
			'$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 . $prod_image;

rename($ImageName, $newfilename);

echo $prod_name . ' added to the database!';
}

?>

Posted: Mon Mar 27, 2006 7:43 pm
by feyd
The destination you give it is a file system reference, not document root reference, so /images would be the images directory in the root of the server, likely not where you want it. you may want to use $_SERVER['DOCUMENT_ROOT'] to help you specify the correct directory.

Posted: Mon Mar 27, 2006 8:03 pm
by aceconcepts
so regarding the structure of my code, how would i use it to resolve the error message i get?

Posted: Mon Mar 27, 2006 8:37 pm
by aceconcepts
These are the two error messages i get:

Warning: move_uploaded_file(images/animal_party_elephant_mask.gif): failed to open stream: Permission denied in /home/ace/public_html/ace_cart/admin/check_product.php on line 20

Warning: move_uploaded_file(): Unable to move '/tmp/php0B6PYh' to 'images/animal_party_elephant_mask.gif' in /home/ace/public_html/ace_cart/admin/check_product.php on line 20

Posted: Mon Mar 27, 2006 11:02 pm
by feyd
For that, you will need to set the permissions of the images directory so that php can write to it. The setting you need can easily vary from server to server due to different ways of setting them up. The easiest, and most insecure is 0777. I would not recommend using that, but it will get it working. The problem would be other users on the server or allowing a "bad" file to be uploaded. But we've gone over the security concerns about that before.

Posted: Tue Mar 28, 2006 3:05 am
by aceconcepts
How would i set the permission of the image directory?

Would this be done through php or in the server cpanel?

I've changed the directory path so that the image is moved to the correct place but now i get this message:

Warning: move_uploaded_file(/home/ace/public_html/ace_cart/admin/images/animal_party_elephant_mask.gif): failed to open stream: Permission denied in /home/ace/public_html/ace_cart/admin/check_product.php on line 23

Warning: move_uploaded_file(): Unable to move '/tmp/phpLk9EgI' to '/home/ace/public_html/ace_cart/admin/images/animal_party_elephant_mask.gif' in /home/ace/public_html/ace_cart/admin/check_product.php on line 23

It does indeed sound like a permission issue. I just don't know how to set or alter the permission.

Posted: Tue Mar 28, 2006 5:09 am
by NeoNmaN

Code: Select all

if (move_uploaded_file($_FILES['prod_image'] ['tmp_name'], $ImageName))
try to use this code

Code: Select all

if (copy($_FILES['prod_image'] ['tmp_name'], $ImageName))
maby ist working :)

and rember to crachse folder and sub folder to chmod 777 becures else you can't upload to the right folder.

Posted: Tue Mar 28, 2006 5:54 am
by aceconcepts
how do i change folders to chmod 777?

Posted: Tue Mar 28, 2006 9:25 am
by feyd
move_uploaded_file() is the only "copy" you should ever use with uploads. Try using copy() or rename() with safe_mode on, you'll find out why.

As for chmodding the folder, you could use your favorite FTP often. If not, cpanel can generally do it too. Again, I'd suggest to avoid 777 as it's very insecure. Read the previously linked thread for details on how to decide the best permission level.

Worst case, you can run the permutations of permissions. There are 511. :)