Jump to content

Keep receiving a failed to open stream (for image) error


Dave2136

Recommended Posts

Hi I am really frustrated because I got my php code working fine and then I had to make some minor changes to my html form and now the php is not working. I am reciec=ving this message:

 

Warning: move_uploaded_file(upload/carey.bmp) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/19/6550319/html/ipad/listing.php on line 35

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpIEUQLo' to 'upload/carey.bmp' in /home/content/19/6550319/html/ipad/listing.php on line 35

Sorry, there was a problem uploading your file.03/10/11 : 20:50:08

 

I wrote it twice because it keeps coming out twice. I guess it means that it cant upload the image because there is no image but I really dont understand why. I havent changed the image part of the form. Does anyone have any ideas? It is much appreciated.

 

Here is the code

 

<?php




//This is the directory where images will be saved
$target = "upload/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$price=$_POST['price'];
$pic=($_FILES['photo']['name']);
$pic2=($_FILES['phototwo']['name']);
$pic3=($_FILES['photothree']['name']);
$pic4=($_FILES['photofour']['name']);
$description=$_POST['iPadDescription'];
$condition=$_POST['condition'];
$gig=$_POST['giga'];
$yesg=$_POST['yesg'];
$fname=$_POST['firstName'];
$lname=$_POST['lastName'];
$email=$_POST['email'];



// Connects to your Database
mysql_connect ("taken out for security", "taken out", "taken out") or die(mysql_error()) ;
mysql_select_db("taken out") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO ipadlist (price,photo,phototwo,photothree,photofour,iPadDescription,condition,giga,yesg,firstName,lastName,email)
VALUES ('$price', '$pic', '$pic2', '$pic3', '$pic4', '$description', '$condition', '$gig', '$yesg', '$fname', '$lname', '$email')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

echo date("m/d/y : H:i:s", time()) 


?>

You need to add a lot of error checking before trying to move the file.  If there was an error with the upload, then of course the file wouldn't exist in the tmp folder.  Therefore, move_uploaded_file() would fail.

 

Take a look at this upload function.  It is old.

function uploadImage($setWidth = 1000, $setHeight = 1000, $filepath = 'upload/') {
	$content = NULL;	
	if($_FILES["file"]["size"] > 900000) { $error[] = "File is to large, submit a smaller image!"; }
	elseif(empty($_FILES["file"]["name"])) { $error[] = "No File Uploaded!"; }
					else
						{
							if ((($_FILES["file"]["type"] == "image/gif")
								|| ($_FILES["file"]["type"] == "image/jpeg")
								|| ($_FILES["file"]["type"] == "image/pjpeg")
								|| ($_FILES["file"]["type"] == "image/png")
								|| ($_FILES["file"]["type"] == "image/jpg")
								|| ($_FILES["file"]["type"] == "image/x-png"))
								&& ($_FILES["file"]["size"] < 900000))
									{
									$image = str_replace(' ', '_', $_FILES['file']['name']);

										if ($_FILES["file"]["error"] > 0)
											{
												$error[] = "*Return Code: " . $_FILES["file"]["error"] . "<br />";
											}
										elseif (file_exists($filepath . $image))
											{
											  $error[] = '*Image exists: ' . $image ;
											}
										else
											{													
															$uploadedfile = $_FILES["file"]["tmp_name"];
															$size = getimagesize($uploadedfile);
															$type = $size['mime'];
															$width = $size[0];
															$height = $size[1];
															$filename = $filepath.$image;
																	if($height > $setHeight || $width > $setWidth) 
																		{ 
																			$newwidth=$setWidth;
																			$newheight=($height/$width)*$setWidth;
																			$tmp=imagecreatetruecolor($newwidth,$newheight);


																				if($size[2] == IMAGETYPE_GIF)
																					{
																					    $src = imagecreatefromgif($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagegif($tmp,$filename,100);
																					}
																				elseif($size[2] == IMAGETYPE_JPEG)
																					{
																					    $src = imagecreatefromjpeg($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagejpeg($tmp,$filename,100);
																					}
																				elseif($size[2] == IMAGETYPE_PNG) 
																					{
																						$src = imagecreatefrompng($uploadedfile);
																						imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
																						imagepng($tmp,$filename,9);
																					}

																		imagedestroy($src);
																		imagedestroy($tmp);
																		return $image;	
																		}
																	else
																		{      
																		   if(move_uploaded_file($uploadedfile, $filename)) {
																			return $image;
																			}
																			$error[] = '*Failed during file movement!';																					
																		}								

											}

									}
								else { $error[] =  "*Invalid file"; }
						}
	return $error;
}

//call function:

//using defaults
$image = uploadImage();

//specify arguments
$image = uploadImage(20,40,'images/upload');


if(!is_array($image)) {
//do database insert; $image holds image name.
}
else {
  foreach($image as $error) {
    echo $error . '<br />';
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.