I seem to be having a problem with my upload script. The script is meant to loop through the code 8 times, each time it checks whether there is an image to be uploaded, if there is then it determines what to call it and moves it to a directory.

This a snippet of the html code it loops through:
HTML Code:
<td>Gallery Image Source 1:</td>
<td><input name="image1" onchange="document.getElementById('ignoreGallery').checked=false;" type="file" />
<input type="hidden" name="deleteImage1" id="deleteImage1" />
<input type="hidden" name="oldImage1" value="" /></td>
Obviously each different snippet of html has different identities such as the next file input would be called "image2"

The first bit determines whether there is an old image that is going to be replaced

Then it tests that form file field if there is an image ready to be uploaded

The next bit loops through the directory where the images are and sees if the oldImage exists and if so deletes it

The the final bit of code does the upload with a certain filename.

Here is my php code:
PHP Code:
$oldImage $_POST['oldImage'.$i];
$_FILES['image'.$i]['tmp_name']) && preg_match('/(?:jpe?g|gif|png)$/i'$_FILES['image'.$i]['name'])) || $_POST['deleteImage'.$i]==1){
                    if (
$handle opendir($uploadsDirectory)) {
                        while (
false !== ($file readdir($handle))) {
                            if (
$file === $oldImage) {
$photoExists $file;
$fieldname 'image'.$i;
is_uploaded_file($_FILES[$fieldname]['tmp_name'])) {
$oldImageName explode('.'$oldImage);
$extension end(explode("."$_FILES[$fieldname]['name']));                        
file_exists($uploadFilename $uploadsDirectory.$oldImageName[0].'.'.$extension)){ 
move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
The main problem is that the script sometimes works and sometimes doesn't depending on whether there has already been an uploaded image. The weird thing is that I do not get any errors. Simply $_FILES['image'.$i]['tmp_name'] does not find any values, apparently there is no image to upload although I know there is.

Any help much appreciated,

Thanks in Advance,