I am trying to get this image uploader for a web forum that I help out with. The code wasn't designed by me. The problem with the code is that it should allow a user to upload 12 files then check each file to make sure they are "each" under 500kb, but the script checks all 12 files together and if the user uploads 1 file that is 450kb and 1 that is 200kb it doesn't work. It doesn't even show the correct error message it is programmed to. I am still learning php and I am lost with this

can someone please help me.
Also the script is set up to run along side vBulletin, and if they are a member of a certain user group they get the 500kb upload and a different watermark if they are a member of another usergroup they get.
Thanks for the help
Code:
<?
$uploadboxes = 12;
$uploadpath = "uploadfiles/";
$watermarkopacity = 75;
$limitedext = array(".gif",".jpg",".jpeg",".GIF",".JPG",".JPEG");
$databasename = "xxxxxxxx";
$dbuser = "xxxxxxxxx";
$dbpass = "xxxxxxxx";
$conn = mysql_connect("localhost", $dbuser, $dbpass);
mysql_select_db($databasename,$conn);
$ui = $_POST[userid];
if($_POST[sb]) { $sb = $_POST[sb]; }
$sizelimit = 350000;
//if($sb) $sizelimit = 500000;
if($sb) $sizelimit = 500000;
?>
<html>
<head>
<title>Lazy**** Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.headpeice {
background-image: url(images/misc/forum_header.gif);
background-repeat: no-repeat;
background-position: right top;
}
.vbmenu_control2
{
background: url(images/misc/bgb.gif) repeat-x top left;
color: #CCCCCC;
font: bold 11px verdana;
padding: 0px 10px 0px 0px;
}
.top {
background-color: #262626;
font-weight: bold;
}
.left {
background-color: #4F4F4F;
}
.right {
background-color: #464646;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
td {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
a {
color: #8EA7D2;
text-decoration: none;
}
a:hover {
color: #8EA7D2;
text-decoration: underline;
}
-->
</style>
<style type="text/css">
<!--
.textbox {
font-size: 10px;
color: #CCCCCC;
background-color: #333333;
font-family: Arial, Helvetica, sans-serif;
}
-->
</style>
</head>
<body onLoad="start();" bgcolor="#646464" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- logo -->
<a name="top"></a>
<table height="150" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<td class="headpeice"> </td>
</tr>
</table>
<table height="45" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<tr>
<td class="vbmenu_control2"><a href="http://www.lazy****.net/store.php"src="images/misc/store1.gif" border="0" alt="Store" /></a>
</td>
<td class="vbmenu_control2" width="100%" align="right"><a href="http://www.lazy****.net"><img src="images/misc/lazylogo.gif" border="0" /></a>
</td>
</tr>
</table>
<br>
<table width="500" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#333333" style="border: 1px solid #AAAAAA">
<tr>
<td><a href="upload.php?ui=<? echo $_POST[userid]; if($sb) echo "&sb=".$sb; ?>"><strong>Upload More</strong></a>
<strong>- <a href="viewuploads.php?ui=<? echo $ui; if($sb) echo "&sb=".$sb; ?>">View Uploads</a>
<? if($sb) { ?>- <a href="viewuploads.php?ui=<? echo $ui; if($sb) echo "&sb=".$sb."&v=1"; ?>">My Pictures</a><? } ?> </strong>
<p><br>
<?
for($x=0;$x<$uploadboxes;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
$file_size = $_FILES['uploadFile'. $x]['size'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$file_name = str_replace(" ", "", $file_name); // Gets rid of spaces.
$file_name = str_replace("%20", "", $file_name);
$ext = strrchr($file_name,'.'); // Gets file extension.
while ((file_exists($uploadpath.$file_name)) && $file_name) {
$fileexplode = explode(".",$file_name);
$file_name = $fileexplode[0]."_.".$fileexplode[1];
}
if ($file_size > $sizelimit) {
echo $file_name; ?>
- ERROR: This file exceeds the size limit of <? echo $sizelimit / 1000; ?>.<br>
<?
} elseif (!in_array($ext,$limitedext) && $file_name) {
echo $file_name; ?>
- ERROR: The file must be a GIF, JPG or JPEG.<br>
<?
} else {
if($file_name) {
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$uploadpath.$file_name) or die("<br><br><b>There was a permissions problem with uploading. Please contact the site administrator.</b>");
}
// check if successfully copied
if($copy && $file_name){
// chmod($uploadpath.$locationselect.$file_name, 0777); // octal; correct value of mode
if($ext == ".jpg" || $ext == ".jpeg" || $ext == ".JPG" || $ext == ".JPEG") {
// Watermarking process if the file is a JPEG.
$baseimage = ImageCreateFromJpeg($uploadpath.$file_name);
if($sb) { $logoImage = ImageCreateFromGif("watermark_og.gif"); }
else {
$logoImage = ImageCreateFromGif("watermark.gif");
}
$ix = imagesx($baseimage) - imagesx($logoImage);
$iy = imagesy($baseimage) - imagesy($logoImage);
ImageCopyMerge($baseimage,$logoImage,$ix,$iy,0,0,imagesx($logoImage),imagesy($logoImage),$watermarkopacity);
//header ("Content-type: image/gif");
ImageJpeg($baseimage, $uploadpath.$file_name);
}// Ends watermarking process
if(!$_POST[private]) { $setprivate = 0; }
if($_POST[private] == 1) { $setprivate = 1; }
$sql = "INSERT INTO imgstorage (userid, image_name, timeup, private) VALUES ('".$_POST[userid]."', '".$file_name."', '".time()."', '".$setprivate."')";
$result = mysql_query($sql,$conn);
$fileurl[$x] = "http://www.lazy****.net/".$uploadpath.$file_name;
echo $file_name; ?>
- Uploaded.<br>
<?
} /* If $copy */ else{
if($file_name) {
echo "$file_name could not be uploaded!<br>";
}
}
}
} // end of loop
?>
</p>
<textarea name="textarea" cols="80" rows="5" class="textbox"><?
for($x=0;$x<$uploadboxes;$x++){
if($fileurl[$x] != "") {
echo "[IMG]".$fileurl[$x]."[/IMG]\n";
}
}
?></textarea></td>
</tr>
</table>
<br>
<? for($x=0;$x<$uploadboxes;$x++){
if($fileurl[$x] != "") {
$ext = strrchr($fileurl[$x],'/');
$ext = str_replace("/","",$ext);
?>
<table width="500" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#333333" style="border: 1px solid #AAAAAA">
<tr>
<td><strong><? echo $ext; ?></strong>
<hr size="1">
<img src="<? echo $fileurl[$x]; ?>"></td>
</tr>
</table><br>
<? }
} ?>