Strange problem when deleting images from form

Hi guys.
The past couple of days i ahve been working on a simple image upload form to attach files to another contact form.
The problem with the form is if you upload say 4 images the images are displayed pretty much like the file attachment function on the forum.
But for some strange reason the wrong file for deletion is being passed and i cant see were the error is.
Here is the form
http://www.innovativedesigns.me.uk/test/php/addImage.php
You will see if you delete any images that is not the last then it does not get deleted, infact it’s the last image in the row that is deleted.
Look at the output all id’s seem fine.
Here’s the script.


if(empty($_POST["upID"])){
$upID = 1;
$fPath="../temp/".$upID;
if (!file_exists($fPath)) {
mkdir('../temp/'.$upID,0777);
}
?>
<p align="center" class="style1">Max 10 images can be attached. </p>
<p align="left" class="style3">
<table width="552" border='1' align="center">
 
<form name="form" FORM ENCTYPE='multipart/form-data' ACTION='addImage.php' METHOD="POST" onSubmit="return validate1();">
<tr>
<td width="172" class="style1">Please select image: </td>
<td width="313"><input name="file" type="file" class="style1" id="file" style="font-size:9px " size="60">
<input name="upID" type="hidden" value="<? echo $upID;?>"></td>
<td width="45"><input name='upload' type='submit' class="style1" id="upload" style="font-size:9px " value='Upload'></td>
</tr>
</form>
</table>
<p>&nbsp;</p>
<?
}else{
$upID = $_POST['upID']; 
if(!empty($_POST["dele"])){
echo "toDel = ".'../temp/'.$upID.'/'.$_POST['toDele'];
unlink('../temp/'.$upID.'/'.$_POST['toDele']);
?> 
<p align="center" class="style3">Max nearly reached. </p>
<p align="left" class="style3">
<table width="552" border='1' align="center">
<form name="form" FORM ENCTYPE='multipart/form-data' ACTION='addImage.php' METHOD="POST">
 
<tr>
<td width="172" class="style1">Please select image: </td>
<td width="313"><input name="file" type="file" class="style1" id="file" style="font-size:9px " size="40"></td>
<td width="45"><input name='upload' type='submit' class="style1" id="upload" style="font-size:9px " value='Upload' ></td>
<input name="upID" type="hidden" value="<? echo $upID; ?>">
</tr>
<tr>
<td height="26" colspan="3"><table width="63%" border="1" cellpadding="0" cellspacing="0" bordercolor="#D4D0C8">
<tr>
<td bordercolor="#999999" scope="col"><table width="96%" border="0" cellpadding="0" cellspacing="0" bordercolor="#666666">
<tr>
<td scope="col"><? 
$i=0;
if ($handle = opendir('../temp/'.$upID.'/')) {
while (false !== ($file1 = readdir($handle))) {
if ($file1 != "." && $file1 != "..") {
$i++;
echo " 
<tr>
<td scope='col'><table width='100%' border='0'>
<tr>
<td width='4%' scope='col'><img src='jpg.gif' width='16' height='16' /></td>
<td width='30%' scope='col'><a href='../temp/".$upID.'/'.$file1."' target='_blank'>".$file1."</a></td>
<td width='81%' scope='col'><input name='dele' type='submit' id='dele' style='font-size:9px' value='Remove' />
<input name='toDele' type='hidden' value='".$file1."' /><input name='upID' type='hidden' value='$upID'></td>
</tr>
</table></td>
</tr>
";
}
}
closedir($handle);
}
?></td>
</tr>
</table></td>
</tr>
</table>
<div align="center">
<? if(!empty($error)){ echo $error; } ?>
</div></td>
<tr>
<td colspan="2"><table width="100%" border="0" cellpadding="1" cellspacing="0" bordercolor="#000000">
<tr>
<td height="20" class="style1" scope="col">You currently have <? echo $i; ?> images attached. </td>
</tr>
</table></td>
<td><input name='close2' type='button' class="style1" id="close2" style='font-size:9px' value='Close'></td>
</form>
</table>
</p>
<?
}else{
$types_array = array("image/jpeg","image/pjpeg");
if (!in_array($_FILES['file']['type'], $types_array)) {
$error = "<font color='#CC0033'>".$_FILES['file']['name']." is an incorrect file type<br/> File must be a jpg</font>";
 
}else{
$xName =$_FILES['file']['name'];
$xDestination = '../temp/'.$upID."/";
if(move_uploaded_file($_FILES['file']['tmp_name'], $xDestination.$xName)) {
$imgXL = $xName;
}
}
// ----------------------------//
 
?>
<p align="center" class="style3">Max nearly reached. </p>
<p align="left" class="style3">
<table width="552" border='1' align="center">
<form name="form" FORM ENCTYPE='multipart/form-data' ACTION='addImage.php' METHOD="POST">
 
<tr>
<td width="172" class="style1">Please select image: </td>
<td width="313"><input name="file" type="file" class="style1" id="file" style="font-size:9px " size="40"></td>
<td width="45"><input name='upload' type='submit' class="style1" id="upload" style="font-size:9px " value='Upload' ></td>
<input name="upID" type="hidden" value="<? echo $upID; ?>">
</tr>
<tr>
<td colspan="3"><table width="63%" border="1" cellpadding="0" cellspacing="0" bordercolor="#D4D0C8">
<tr>
<td bordercolor="#999999" scope="col"><table width="96%" border="0" cellpadding="0" cellspacing="0" bordercolor="#666666">
<tr>
<td scope="col"><? 
$i=0;
if ($handle = opendir('../temp/'.$upID.'/')) {
while (false !== ($file1 = readdir($handle))) {
 
if ($file1 != "." && $file1 != "..") {
$i++;
echo " 
<tr>
<td scope='col'><table width='100%' border='0'>
<tr>
<td width='4%' scope='col'><img src='jpg.gif' width='16' height='16' /></td>
<td width='30%' scope='col'><a href='../temp/".$upID.'/'.$file1."' target='_blank'>".$file1."</a></td>
<td width='81%' scope='col'><input name='dele' type='submit' id='dele' style='font-size:9px' value='Remove' />
<input name='toDele' type='hidden' value='".$file1."'/><input name='upID' type='hidden' value='$upID'></td>
</tr>
</table></td>
</tr>
";
}
}
closedir($handle);
}
?></td>
</tr>
</table></td>
</tr>
</table>
 
<div align="center">
<? if(!empty($error)){ echo $error; } ?>
</div></td>
<tr>
<td colspan="2"><table width="100%" border="0" cellpadding="1" cellspacing="0" bordercolor="#000000">
<tr>
<td height="20" class="style1" scope="col">You currently have <? echo $i; ?> images attached. </td>
</tr>
</table></td>
<td><input name='close2' type='button' class="style1" id="close2" style='font-size:9px' value='Close'></td>
</form>
</table>
</p>
<?
}
}
?>

I feel i am missing the obvious.