Renaming files in "for()"

Hi guys.
i am working on an gallery admin system and have hit a little hurdle.
Image files on the server are stored named as 1_thumb.jpg, 1_main.jpg, 2_thumb.jpg, 2_main.jpg and so on…
my problem is when it comes to deleting some files.
With image files number 1-8 i delete image set 3 so this leaves in the folder images numbered 1-8 with image set three just been deleted. i am trying to rename the remainder of the files so that they are number 1-7 give that 3 has been deleted.
At this point the script freeze’s at the point of the that has just been deleted.


$editID  = $_POST['editID'];
$fileID  = $_POST['fileID'];
$fileDIR = "../media/images/".$editID;
//Delete image
$delThumb = unlink($fileDIR."/".$fileID.'_thumb.jpg');
$delMain = unlink($fileDIR."/".$fileID.'_main.jpg');
//Get new image count
if ($handle = opendir('../media/images/'.$editID.'/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
    if(substr_count($file, '_main.jpg')==1){
   //
   }else
   if(substr_count($file, '_popup.jpg')==1){
  //
   }else 
   if(substr_count($file, 'spotlite.jpg')==1){
  //
   }else{
           $i++;
   rename($fileDIR ."/". $i . "_thumb.jpg", $fileDIR ."/". $i ."_thumb.jpg");
   rename($fileDIR ."/". $i . "_main.jpg", $fileDIR ."/". $i . "_main.jpg");
         }
  }
    }
}
closedir($handle);
echo "next i =".$i;
//Append file names
/*
for($j=1;$j<$i;$j++){
} 
*/
//
}

i feel i should no the answer to this but i just can’t see the wood for the tree’s.
Any help much appreciatted.