Hi, Risingstar here, some of you may recognize me from such threads as "File Upload Script: Huge Problem" or "Returning True or False from a Database Query".
Really sorry, but I try as hard as I can to figure out a problem before I make a thread. I promise I'm getting really close to the end of these thread because my project is slowly drawing closer to an end.
Anyways, my problem this time is that I'm trying to upload and query 2 files from the same form. It was all working fine and dandy, but I had to add a system where if the imageType was comic, it had a bigger file limit. I think that might be where I screwed something up.
Here's the code:
<?php
$link = mysql_connect('localhost', 'spowpow1_Sean', '<password>');
if (!$link)
{
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('spowpow1_gmodpic', $link);
if (!$db_selected)
{
die ("Database not selected : " . mysql_error());
}
//Making sure all the inputs are the right length and type
function sanityCheck($string, $type, $length)
{
$type = 'is_'.$type;
if(!$type($string))
{
return FALSE;
}
elseif(empty($string))
{
return FALSE;
}
elseif(strlen($string) > $length)
{
return FALSE;
}
else
{
return TRUE;
}
}
//Checking to see which file uploaders have content
//Need to check for ALL the possible JPEG file types because IE is a huge *****
//Upload Field 1
if ($_FILES['file']['name'] != "")
{
if ($_POST['imageType'] == "comics")
{
if ($_FILES['file']['size'] <= 2000000)
{
if ($_FILES['file']['type'] == 'image/pjpeg' || ($_FILES['file']['type'] == 'image/jpeg' || ($_FILES['file']['type'] == 'image/jpg' || ($_FILES['file']['type'] == 'image/png'))))
{
$fileSet = 1;
}
else
{
echo("<p>Incorrect file type. Please only upload PNGs and JPEGs.</p>");
}
}
}
elseif ($_POST['imageType'] == "pose" || ($_POST['imageType'] == "contraption" || ($_POST['imageType'] == "artistic")))
{
if ($_FILES['file']['size'] <= 500000)
{
if ($_FILES['file']['type'] == 'image/pjpeg' || ($_FILES['file']['type'] == 'image/jpeg' || ($_FILES['file']['type'] == 'image/jpg' || ($_FILES['file']['type'] == 'image/png'))))
{
$fileSet = 1;
}
else
{
echo("<p>Incorrect file type. Please only upload PNGs and JPEGs.</p>");
}
}
else
{
$fileSet = 0;
$fileSize = $_FILES['file']['size'] / 100;
echo("<p>File too big! Maximum image upload size is 500 KB. Your file is" . $fileSize . " KB!</p>");
}
}
}
else
{
$fileSet = 0;
echo("Please use the top upload field first.");
}
//Upload Field 2
if ($_FILES['file2']['name'] != "")
{
if ($_POST['imageType2'] == "comics")
{
if ($_FILES['file2']['size'] <= 2000000)
{
if ($_FILES['file2']['type'] == 'image/pjpeg' || ($_FILES['file2']['type'] == 'image/jpeg' || ($_FILES['file2']['type'] == 'image/jpg' || ($_FILES['file2']['type'] == 'image/png'))))
{
$fileSet2 = 1;
}
else
{
echo("<p>Incorrect file type. Please only upload PNGs and JPEGs.</p>");
}
}
}
elseif ($_POST['imageType2'] == "pose" || ($_POST['imageType2'] == "contraption" || ($_POST['imageType2'] == "artistic")))
{
if ($_FILES['file2']['size'] <= 500000)
{
if ($_FILES['file2']['type'] == 'image/pjpeg' || ($_FILES['file2']['type'] == 'image/jpeg' || ($_FILES['file2']['type'] == 'image/jpg' || ($_FILES['file2']['type'] == 'image/png'))))
{
$fileSet2 = 1;
}
else
{
echo("<p>Incorrect file type. Please only upload PNGs and JPEGs.</p>");
}
}
else
{
$fileSet2 = 0;
$fileSize2 = $_FILES['file2']['size'] / 100;
echo("<p>File too big! Maximum image upload size is 500 KB. Your file is" . $fileSize2 . " KB!</p>");
}
}
}
else
{
$fileSet2 = 0;
}
//Setting global variables
$author = $_COOKIE['userName'];
$fDate = date('l jS \of F Y');
$fTime = date('h:i:s A');
//Begin uploading... File 1
if ($fileSet == 1)
{
if(empty($_POST['title']) == FALSE && sanityCheck($_POST['title'], 'string', 30) != FALSE)
{
$title = $_POST['title'];
}
else
{
echo("<p>You need to have a title for your first picture!</p><br />");
}
if(empty($_POST['desc']) == FALSE && sanityCheck($_POST['desc'], 'string', 250) != FALSE)
{
$oldDesc = $_POST['desc'];
$desc = escapeshellcmd($oldDesc);
}
else
{
$desc = $_POST['desc'];
}
if ($_POST['imageType'] == "comics")
{
$imgType = "Comic Strip";
$fName = "comics/" . rand() . ".jpg";
$path = "uploads/" . $fName;
}
elseif ($_POST['imageType'] == "pose")
{
$imgType = "Ragdoll Pose";
$fName = "poses/" . rand() . ".jpg";
$path = "uploads/" . $fName;
}
elseif ($_POST['imageType'] == "contraption")
{
$imgType = "Contraption";
$fName = "contraption/" . rand() . ".jpg";
$path = "uploads/" . $fName;
}
elseif ($_POST['imageType'] == "artistic")
{
$imgType = "Artistic";
$fName = "artistic/" . rand() . ".jpg";
$path = "uploads/" . $fName;
}
$file_name = $_FILES['file']['name'];
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $path))
{
echo "<p>Picture 1 successfully uploaded!</p><br />";
}
else
{
echo "<p>There was an error during upload. Please try again.</p><br />";
}
}
$query = sprintf("INSERT INTO images4 (title, filePath, category, description, author, date, time)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($title),
mysql_real_escape_string($fName),
mysql_real_escape_string($imgType),
mysql_real_escape_string($desc),
mysql_real_escape_string($author),
mysql_real_escape_string($fDate),
mysql_real_escape_string($fTime));
if (!mysql_query($query))
{
echo("<p>Query Failed " . mysql_error() . "</p><br />");
}
else
{
echo("<p>Picture 1 successfully queried!</p><br /><br />");
}
}
//File 2
if ($file2Set == 1)
{
if(empty($_POST['title2']) == FALSE && sanityCheck($_POST['title2'], 'string', 30) != FALSE)
{
$title2 = $_POST['title2'];
}
else
{
echo("<p>You need to have a title for your second picture!</p><br />");
}
if(empty($_POST['desc2']) == FALSE && sanityCheck($_POST['desc2'], 'string', 250) != FALSE)
{
$oldDesc2 = $_POST['desc2'];
$desc2 = escapeshellcmd($oldDesc2);
}
else
{
$desc2 = $_POST['desc2'];
}
if ($_POST['imageType2'] == "comics")
{
$imgType2 = "Comic Strip";
$fName2 = "comics/" . rand() . ".jpg";
$path2 = "uploads/" . $fName2;
}
elseif ($_POST['imageType2'] == "pose")
{
$imgType2 = "Ragdoll Pose";
$fName2 = "poses/" . rand() . ".jpg";
$path2 = "uploads/" . $fName2;
}
elseif ($_POST['imageType2'] == "contraption")
{
$imgType2 = "Contraption";
$fName2 = "contraption/" . rand() . ".jpg";
$path2 = "uploads/" . $fName2;
}
elseif ($_POST['imageType2'] == "artistic")
{
$imgType2 = "Artistic";
$fName2 = "artistic/" . rand() . ".jpg";
$path2 = "uploads/" . $fName2;
}
$file_name2 = $_FILES['file2']['name'];
if(is_uploaded_file($_FILES['file2']['tmp_name']))
{
if(move_uploaded_file($_FILES['file2']['tmp_name'], $path2))
{
echo 'just moved file 2 to ' . $path2 . '<br>';
echo 'size of file 2: ' . filesize($path2) . '<br>';
echo "<p>Picture 2 successfully uploaded!</p><br />";
}
else
{
echo "<p>There was an error during upload. Please try again.</p><br />";
}
}
$query2 = sprintf("INSERT INTO images4 (title, filePath, category, description, author, date, time)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($title2),
mysql_real_escape_string($fName2),
mysql_real_escape_string($imgType2),
mysql_real_escape_string($desc2),
mysql_real_escape_string($author),
mysql_real_escape_string($fDate),
mysql_real_escape_string($fTime));
if (!mysql_query($query2))
{
echo("<p>Query Failed " . mysql_error() . "</p><br />");
}
else
{
echo("<p>Picture 2 successfully queried!</p><br /><br />");
}
}
mysql_close($link);
?>
Sorry the code is so long, if I could make it shorter and easier for you guys, I would.
If anyone has any idea what's going on here, I would absolutely love to hear your solutions.
Thanks!