My goal is to get the script to upload a photo, resize it to maximum width 400px, create a thumbnail of maximum width 135px, then insert each filename into the database. The errors I get are:

Here is the code:

<?php

session_start();

require_once('Connections/DBConnect.php');

if (!function_exists("GetSQLValueString")) 
{
	function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
	{
		$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
		$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
		switch ($theType) 
		{
    		case "text":
      			$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      		break;    
case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadphoto")) { if (array_key_exists('upload', $_POST)) { // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $typeOK = false; // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($typeOK) { switch($_FILES['image']['error']) { case 0: // define constants define('PHOTO_DIR', 'C:/wamp/www/test/photos/'); define('MAX_PHOTO_WIDTH', 400); define('MAX_PHOTO_HEIGHT', 750); define('THUMB_DIR', 'C:/wamp/www/test/photos/thumbs/'); define('MAX_THUMB_WIDTH', 135); define('MAX_THUMB_HEIGHT',200); // process the uploaded image if (is_uploaded_file($_FILES['image']['tmp_name'])) { $original = $_FILES['image']['tmp_name']; // begin by getting the details of the original photo list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio for the new large photo if ($width <= MAX_PHOTO_WIDTH && $height <= MAX_PHOTO_HEIGHT) { $photo_ratio = 1; } elseif ($width > $height) { $photo_ratio = MAX_PHOTO_WIDTH/$width; } else { $photo_ratio = MAX_PHOTO_HEIGHT/$height; } // calculate the scaling ratio for the thumbnail if ($width <= MAX_THUMB_WIDTH && $height <= MAX_THUMB_HEIGHT) { $thumb_ratio = 1; } elseif ($width > $height) { $thumb_ratio = MAX_THUMB_WIDTH/$width; } else { $thumb_ratio = MAX_THUMB_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/'); $name = preg_replace($imagetypes, '', basename($_FILES['image']['name'])); // move the temporary file to the upload folder $photo_moved = move_uploaded_file($original, PHOTO_DIR.$_FILES['image']['name']); $thumb_moved = move_uploaded_file($original, THUMB_DIR.$_FILES['image']['name']); if ($photo_moved && $thumb_moved) { $result = $_FILES['image']['name'].' successfully uploaded; '; $photo_original = PHOTO_DIR.$_FILES['image']['name']; $thumb_original = THUMB_DIR.$_FILES['image']['name']; } else { $result = 'Problem uploading '.$_FILES['image']['name'].'; '; } // create an image resource for the original switch($type) { case 1: $photo_source = @ imagecreatefromgif($photo_original); $thumb_source = @ imagecreatefromgif(thumb_original); if ((!$photo_source) || (!$thumb_source)) { $result = 'Cannot process GIF files. Please use JPEG or PNG.'; } break; case 2: $photo_source = imagecreatefromjpeg($photo_original); $thumb_source = imagecreatefromjpeg($thumb_original); break; case 3: $photo_source = imagecreatefrompng($photo_original); $thumb_source = imagecreatefrompng($thumb_original); break; default: $photo_source && $thumb_source = NULL; $result = 'Cannot identify file type.'; } // make sure the image resource is OK if ((!$photo_source) || (!$thumb_source)) { $result = 'Problem copying original photo.'; } else { // calculate the dimensions of the new large photo $photo_width = round($width * $ratio); $photo_height = round($height * $ratio); // calculate the dimensions of the thumbnail $thumb_width = round($width * $ratio); $thumb_height = round($height * $ratio); // create an image resource for the new large photo $photo = imagecreatetruecolor($photo_width, $photo_height); // create an image resource for the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); //create the resized new large photo imagecopyresampled($photo, $source, 0, 0, 0, 0, $photo_width, $photo_height, $width, $height); // create the resized thumbnail imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); // save the resized copy switch($type) { case 1: if (function_exists('imagegif')) { $photo_success = imagegif($photo, PHOTO_DIR.$name.'.gif'); $thumb_success = imagegif($thumb, THUMB_DIR.$name.'_thumb.gif'); $photo_name = $name.'.gif'; $thumb_name = $name.'_thumb.gif'; } else { $photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 50); $thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.gif', 50); $photo_name = $name.'.jpg'; $thumb_name = $name.'_thb.jpg'; } break; case 2: $photo_success = imagejpeg($photo, PHOTO_DIR.$name.'.jpg', 100); $thumb_success = imagejpeg($thumb, THUMB_DIR.$name.'_thumb.jpg', 100); $photo_name = $name.'.jpg'; $thumb_name = $name.'_thb.jpg'; break; case 3: $photo_success = imagepng($photo, PHOTO_DIR.$name.'.png'); $thumb_success = imagepng($thumb, THUMB_DIR.$name.'_thumb.png'); $photo_name = $name.'.png'; $thumb_name = $name.'_thumb.png'; } if ($photo_success && $thumb_success) { $insertSQL = sprintf("INSERT INTO tblmembergalleryphoto (galleryid, photoname, thumbname) VALUES (%s, %s, %s)", GetSQLValueString($_SESSION['galleryid'], "int"), GetSQLValueString($photo_name, "text"), GetSQLValueString($thumb_name, "text")); mysql_select_db($database_conndb, $conndb); $Result1 = mysql_query($insertSQL, $conndb) or die(mysql_error()); $result .= "$photo_name and $thumb_name created and uploaded successfully. Add another photo or Click <a href='home.php'>here</a> to go back to the administrator home page."; } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($photo_source); imagedestroy($thumb_source); imagedestroy($photo); imagedestroy($thumb); } } break; case 3: $result = "Error uploading $file. Please try again."; default: $result = "System error uploading $file. Contact webmaster."; } } elseif ($_FILES['image']['error'] == 4) { $result = 'No file selected'; } else { $result = "$file cannot be uploaded. Acceptable file types: gif, jpg, png."; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Photo Upload</title> </head> <body> <div align="center"> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p>$result</p>"; } ?> </div> <div align="center"> <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="uploadphoto" id="uploadphoto"> <p> <label for="image">Select an Image to Upload to the Gallery:</label> <br /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> <input type="hidden" name="MM_insert" value="uploadphoto" /> </p> </form> </div> </body> </html>

    I apologize, but my previous post was too long so here are the errors I receive when trying to run this script:

    "Undefined variable: photo_original in C:\wamp\www\test\addphoto.php on line 187

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 187

    Undefined variable: thumb_original in C:\wamp\www\test\addphoto.php on line 188

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 188

    Problem copying original photo."

      it seems one error leads to the next, which is usually the case with image processing. It seems you are calling $photo_original on line 187, whilst it is not defined. The error is before that. However, since your code is quite long, I'll let you do the digging. :p

        It turns out I was missing a $ for one of the variables. Now I get the exact same errors but on lines 141 and 142. Those lines are right after case 2 where it tries to run createimagefromjpeg. It appears $photo_original and $thumb_original are not being declared which means that the original files are not getting moved (move_uploaded_file is not being executed). Any advice on this? I've literally wasted a weekend on this page alone.

          echo $type before going into the switch. I'm expecting this to not match the cases.

            I also echoed the $photo_moved and $thumb_moved variables above (after the move_uploaded_files) and got a 1 back if that helps troubleshooting at all.

              within this if:

              					if ($photo_moved && $thumb_moved)
              

              what do you get if you echo the $photo_original and $thumb_original vars?
              also try doing a print_r($_FILES) there, as well as echoing the DEFINES PHOTO_DIR and THUMB_DIR

                the 1 most likely represents a TRUE boolean which is acquired somewhere through a function return.

                  C:/wamp/www/test/photos/C:/wamp/www/test/photos/thumbs/Array ( [image] => Array ( [name] => _Ferrari-458-Italia-1-lg.jpg [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\php232D.tmp [error] => 0 [size] => 193609 ) )

                  This is what I get when I type"
                  echo PHOTO_DIR;
                  echo THUMB_DIR;
                  print_r($_FILES);

                    When I only echo the move_uploaded_file for $thumb_moved I get no value at all. There should be a 1 in there just like when I echoed $photo_moved

                      ok the vars seem fine. Ferrari rocks.
                      it is weird though that you don't get the 1 as you said. if you don't get anything, it's bool false, which does not echo a 0 but echoes nothing. that would mean the function would have returned false.

                      however, this should have given an error message.

                      if you put the echoes in the IF statement, then we should be fine, as the IF validates both vars.

                      So within that same if, what do you get when you echo the $photo_original and $thumb_original vars?

                        I keep getting a parse error, where exactly should I put the echo statements? Yes, I am a die hard Ferrari fan. It is my life goal to own one. You should check out www.ferrarichat.com

                          I would place the echoes here:

                          if ($photo_moved && $thumb_moved)
                          					{
                          	  					$result = $_FILES['image']['name'].' successfully uploaded; ';
                          	  					$photo_original = PHOTO_DIR.$_FILES['image']['name'];
                          						$thumb_original = THUMB_DIR.$_FILES['image']['name'];
                          
                          echo $photo_original.'<br>'.$thumb_original;
                          
                            				}
                          
                          

                            I didn't get anything back, just the usual errors.

                              very well. now try this:

                              var_dump($photo_moved);
                              var_dump($thumb_moved);
                              if ($photo_moved && $thumb_moved)
                                                  {
                                                        $result = $_FILES['image']['name'].' successfully uploaded; ';
                                                        $photo_original = PHOTO_DIR.$_FILES['image']['name'];
                                                      $thumb_original = THUMB_DIR.$_FILES['image']['name'];
                              
                                                } 
                              

                                I got: bool(true) bool(false) along with the following usual errors:

                                Notice: Undefined variable: photo_original in C:\wamp\www\test\addphoto.php on line 144

                                Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 144

                                Notice: Undefined variable: thumb_original in C:\wamp\www\test\addphoto.php on line 145

                                Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in C:\wamp\www\test\addphoto.php on line 145

                                Problem copying original photo.

                                  ok that tells us there is indeed an issue with the thumb. I guess you said that before somewhere? not sure though.

                                  can you confirm that the thumb dir is available for writing, and exists?

                                    Yes, the thumbs directory exists and is writable. I've been analyzing this code and am seriously stunned that I cannot figure it out. It's like it doesn't want to create that variable ($thumb_moved) for some reason.

                                      oh I know I know I know

                                      possibly

                                      you're doing move_uploaded_file, twice! you're trying to copy it this way, but that doesn't work, because after the first move, there is no uploaded file.

                                      instead, copy the file to the thumb location after the first move_uploaded_file.