After reviewing many many scripts I have managed to get this far. This script validates file size, mime type, extension and uploads a jpg, gif, or png file to the pic/ dir, creates a new resized thumbnail file copied to the thumb/ dir (using GD Lib), and stores the url and file info of the respective files in the db. Seems everything is working well except when there is no file to be uploaded, which should be validated by if(isset($_FILES['picture'])), the queries at the bottom to insert/update the pic and thumb url's still execute and I end up with url's in my db to files that do not exist.
Also, are there any other extension names that I should include for gif and png, and am I validating the MIME types and extensions correctly?
$message1 = NULL;
$message2 = NULL;
$message3 = NULL;
$idir = "pic/"; // Path To Images Directory
$tdir = "thumb/"; // Path To Thumbnails Directory
$twidth = "50"; // Maximum Width For Thumbnail Images
$theight = "50"; // Maximum Height For Thumbnail Images
if(isset($_POST['continue'])) {
if(isset($_FILES['picture'])) {
require_once ('./mysql_connect.php');
$query = "SELECT obit_id FROM obituary ORDER BY obit_id DESC LIMIT 1";
$result = @mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$obit_id = $row[0];
$extension = explode ('.', $_FILES['picture']['name']);
$ext = $extension[1];
$uid = mysql_insert_id();
$url = $uid.'.'.$ext; // Set $url To Equal The Filename For Later Use
$size = ($_FILES['picture']['size'] / 1024);
if ($_FILES['picture']['size'] < 131072) {
if ($_FILES['picture']['type'] == "image/jpg" || $_FILES['picture']['type'] == "image/jpeg" || $_FILES['picture']['type'] == "image/pjpeg") { //Handle JPEG
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
} else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
if ($_FILES['picture']['type'] == "image/gif") { //Handle GIF
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefromgif("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagegif($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
} else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
if($_FILES['picture']['type'] == "image/png" || $_FILES['picture']['type'] == "image/x-png") { //Handle PNG
$copy = copy($_FILES['picture']['tmp_name'], "$idir" . "$url"); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
$message1 .= 'Image uploaded successfully.'; // Was Able To Successfully Upload Image
$simg = imagecreatefrompng("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagepng($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
$message2 .= 'Image resized successfully.<br><br>'; // Resize successful
} else {
$message2 .= 'Unable to resize image.<br><br>'; // Error Message If Upload Failed
}
} else {
$message1 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> Incorrect file type <font color=\"#8DA900\">.$ext</font>.
Please upload .jpeg, .gif , or .png files only."; // Error Message If Filetype Is Wrong
}
}
}
} else {
$message3 .= "<font color=\"#8DA900\">Picture Not Uploaded.</font> File size <font color=\"#8DA900\">$size KB</font>
is too large. Size Limit: 128KB.<br><br>"; // Error Message If Filetype Is Wrong
}
$query1 = "INSERT INTO pics (file_name, file_size, file_type, file_use, date) VALUES ('{$_FILES['picture']['name']}', '{$_FILES['picture']['size']}', '{$_FILES['picture']['type']}', 'OBIT', NOW())";
$result1 = @mysql_query($query1);
$query2 = "INSERT INTO thumbs (file_name, file_size, file_type, file_use, date) VALUES ('{$_FILES['picture']['name']}', '{$_FILES['picture']['size']}', '{$_FILES['picture']['type']}', 'OBIT', NOW())";
$result2 = @mysql_query($query2);
$query3 = "UPDATE obituary SET picture = './pic/$url' WHERE obit_id = '$obit_id'";
$result3 = @mysql_query($query3);
$query4 = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result4 = @mysql_query($query4);
$row4 = mysql_fetch_array($result4, MYSQL_NUM);
$pic_id = $row4[0];
$query5 = "UPDATE pics SET url = './pic/$url', extension = '$ext' WHERE pic_id = '$pic_id'";
$result5 = @mysql_query($query5);
$query6 = "UPDATE obituary SET thumb = './thumb/$url' WHERE obit_id = '$obit_id'";
$result6 = @mysql_query($query6);
$query7 = "SELECT thum_id FROM thumbs ORDER BY thum_id DESC LIMIT 1";
$result7 = @mysql_query($query7);
$row7 = mysql_fetch_array($result7, MYSQL_NUM);
$thum_id = $row7[0];
$query8 = "UPDATE thumbs SET url = './thumb/$url', extension = '$ext' WHERE thum_id = '$thum_id'";
$result8 = @mysql_query($query8);
}
}
if(isset($message)) {
echo "<font class='txt1'>$message</font>";
} else {
echo "<p>Complete the form to create a new obituary.</p>";
}
if(isset($message2)) {
echo "<font class='txt1'>$message1 | $message2</font>";
} else {
echo "<font class='txt1'>$message3</font>";
}