using chmod

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

using chmod

Post by aceconcepts »

Hi,

I'm trying to upload an image file from an html form.

Here is the code used to upload and move the image file:

Code: Select all

<?php
//AUTHORISE ACCESS TO THIS PAGE
include"auth.inc.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
//$ImageDir = "images/";
//$ImageName = $ImageDir . $prod_image;

$path = "/home/ace/public_html/ace_cart/admin/"; // . $_SERVER['SERVER_NAME'] .
//	strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
	
$ImageDir = chmod($path . "images/", 755);
$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.";
	}
	

if ($_POST['new_type'] == "") {
	
	$prod_type = $_POST['prod_type'];
	
} else {
	
	$prod_type = $_POST['new_type'];
	
}

//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!';
}


header ("Refresh: 0; URL=products.php");

?>

And here is the error message i get:

Warning: chmod(): Operation not permitted in /home/ace/public_html/ace_cart/admin/check_product.php on line 25

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

Warning: move_uploaded_file(): Unable to move '/tmp/phpEl3DKW' to 'animal_party_monkey_mask.gif' in /home/ace/public_html/ace_cart/admin/check_product.php on line 27


What am i doing wrong?
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post by bimo »

Do you have your file permissions set so that files can be modified? The error message says that there's a chmod problem, which says to me that you don't.

see if your webhost has any documentation on how you can change file permissions on their servers. Without the correct permissions settings (777 is the easiest but least secure) I don't think you'll be able to use move_uploaded_file() or rename().
Post Reply