Jump to content

[SOLVED] Image upload error (can't find image)


Recommended Posts

Hi guys,

 

Having an issue with uploading an image, it doesn't seem to upload it, so when it goes to resize them, it can't find it. Can anyone see why it might no be uploading in this code:

 

<?php
if($_POST['action']	==	'add_product'){
//Product Variables
$cat_id	=	$_POST['cat_id'];
$product_name	=	$_POST['product_name'];

//main image
$product_image[0]	=	$_FILES['product_image0'];
//closeup images
$product_image[1]	=	$_FILES['product_image1'];
$product_image[2]	=	$_FILES['product_image2'];
$product_image[3]	=	$_FILES['product_image3'];
$product_image[4]	=	$_FILES['product_image4'];
$product_image[5]	=	$_FILES['product_image5'];
$product_image[6]	=	$_FILES['product_image6'];
$product_image[7]	=	$_FILES['product_image7'];
$product_image[8]	=	$_FILES['product_image8'];

$product_description	=	$_POST['product_description'];
//Image Types
$AllowedImages	=	array('image/jpeg','image/pjpeg');

//Preverification of image filetypes
for ($i=0; $i<count($product_image); $i++){
	if (in_array($product_image[$i]['type'], $AllowedImages)){
		$product_image_allowed = 1; //current image type is allowed
	}
	else {
		$product_image_allowed = 0; //current image type is not allowed
		break; //found an image type that's denied - quit the loop
	}
}

//Verification
if(!empty($cat_id) && !empty($product_name) && !empty($product_description) && $product_image['error'] == 0 && $product_image_allowed == 1){
	//Continue & Add to DB

	//process the images
	for ($i=0; $i<count($product_image); $i++){
		$product_image_name[$i] = md5(microtime()).'.jpg'; //generate the filename

		//Write Original File
		WriteFile($cfg['productimages']['original']['folder'].$product_image_name[$i], file_get_contents($product_image[$i]['tmp_name']));
		//Create medium sized thumbnail
		$ImageFunctions->Open($cfg['productimages']['original']['folder'].$product_image_name[$i]);
		$ImageFunctions->Resize(245,184);
		$ImageFunctions->RawOutput();
		WriteFile($cfg['productimages']['medium']['folder'].$product_image_name[$i], $ImageFunctions->Output['Content']);
			//Get Dimensions
			$Image[$i]['medium']['width']	=	$ImageFunctions->Width;
			$Image[$i]['medium']['height']	=	$ImageFunctions->Height;
		//Create smaill sized thumbnail
		$ImageFunctions->Open($cfg['productimages']['original']['folder'].$product_image_name[$i]);
		$ImageFunctions->Resize(65,49);
		$ImageFunctions->RawOutput();
		WriteFile($cfg['productimages']['small']['folder'].$product_image_name[$i], $ImageFunctions->Output['Content']);
			//Get Dimensions
			$Image[$i]['small']['width']	=	$ImageFunctions->Width;
			$Image[$i]['small']['height']	=	$ImageFunctions->Height;
		//End Image Processing
	}
	?>

 

Error:

Warning: fopen(/home/sfengine/public_html/sfengine/sfengineering.ie/productimages/original/dbe59b51badf7b3d61c00538abc8f0c6.jpg) [function.fopen]: failed to open stream: No such file or directory in /home/sfengine/public_html/admin/includes/global.inc.php on line 68

 

Warning: fwrite(): supplied argument is not a valid stream resource in /home/sfengine/public_html/admin/includes/global.inc.php on line 69

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/sfengine/public_html/admin/includes/global.inc.php on line 70

Unable to open the file: /home/sfengine/public_html/sfengine/sfengineering.ie/productimages/original/dbe59b51badf7b3d61c00538abc8f0c6.jpg

 

global.inc.php:

 

<?php
//Product Image Locations
$cfg['productimages']['small']['folder']	=	'/home/sfengine/public_html/sfengineering.ie/productimages/small/';
$cfg['productimages']['medium']['folder']	=	'/home/sfengine/public_html/sfengine/sfengineering.ie/productimages/medium/';
$cfg['productimages']['original']['folder']	=	'/home/sfengine/public_html/sfengine/sfengineering.ie/productimages/original/';

$cfg['productimages']['small']['url']	=	'http://sfengineering.ie/productimages/small/';
$cfg['productimages']['medium']['url']	=	'http://sfengineering.ie/productimages/medium/';
$cfg['productimages']['original']['url']	=	'http://sfengineering.ie/productimages/original/';

$cfg['basefolder']	=	'/home/sfengine/public_html/sfengine/sfengineering.ie/';
?>

 

 

Thanks in advance,

Nick.

The problem is most likely one of 2 things permissions on the upload folder or a missing enctype on the form.

 

Without the seeing the form I can't tell you if it's wrong but you'll want to make sure you have enctype="multipart/form-data" in the opening form tag.

 

As for permissions the upload folder needs to be writable for the user your script is running as.  Depending on the server environment php could be running as apache,nobody, or a particular user. You should be checking to make sure the file was uploaded before attempting to re-size it. 

 

is_uploaded_file works great for that.

http://us3.php.net/manual/en/function.is-uploaded-file.php

 

My last thought which should have been my first is that make sure the php.ini file allows uploads. file_uploads should be On in the php.ini file and safe_mode Off.

 

Dave Arnold, Training Specialist, [email protected]

http://www.HostMySite.com?utm_source=bb

 

Do you actually have a folder with that exact spelling and capitalization - /home/sfengine/public_html/sfengine/sfengineering.ie/productimages/original/

 

it seems it doesn't upload the file to begin with, hence the error, when I look for it on the server, it isn't there either.
If that statement means you cannot see the ['tmp_name'] uploaded file, that is because you would need to be very fast as it is destroyed when the form processing code ends.

 

If you had a permissions problem, the error would indicate so. If file_get_contents($product_image[$i]['tmp_name']) was failing because the tmp_name file does not exist, the error would indicate so.

The best thing to do is validate each step in the process to verify where the break down is happening.  Once you find the problem fixing it will be easy.

 

Test the value of $product_image[$i]['error'].  If it is equal to 0 move on else use die() to kill the script and output its value. 

 

Just before calling WriteFile() enter:

 

#test for errors before writing file
if($product_image[$i]['error'] != 0)
die('Error on Upload: '. $product_image[$i]['error']);

 

Might want to make it a little nicer for production but this will do the trick.

 

Use http://us.php.net/manual/en/features.file-upload.errors.php to determine the problem based on the error number.  This should be include with all file upload scripts. It makes like easier when things like this pop up.

 

Dave Arnold, Training Specialist, [email protected]

http://www.HostMySite.com?utm_source=bb

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.