Possible to delete and move in same foreach block?

I am trying to replace an image file using a replace radio button and file upload prompt.

The foreach block successfully deletes the old file and updates the table but fails to move the new file to the directory.

Is it possible to delete and move in the same foreach block?

The print_r($file); reveals the correct name of the new file, but the new file was never moved to the directory. Thanks.

<input type=‘radio’ name=‘image[image0]’ value=‘keep’ checked=‘checked’/>
<input type=‘radio’ name=‘image[image0]’ value=‘delete’ />
<input type=‘radio’ name=‘image[image0]’ value=‘replace’ />
<input type=“file” name=“image” />

<input type=‘radio’ name=‘image[image1]’ value=‘keep’ checked=‘checked’/>
<input type=‘radio’ name=‘image[image1]’ value=‘delete’ />
<input type=‘radio’ name=‘image[image1]’ value=‘replace’ />
<input type=“file” name=“image” />

<input type=‘radio’ name=‘image[image2]’ value=‘keep’ checked=‘checked’/>
<input type=‘radio’ name=‘image[image2]’ value=‘delete’ />
<input type=‘radio’ name=‘image[image2]’ value=‘replace’ />
<input type=“file” name=“image” />

if (isset($_POST['submitted']) &&
		($image = $_POST['image'])) {
			
		foreach($image as $imageKey => $imageValue) {
			if ($imageValue == 'replace') {
				$query = "SELECT $imageKey FROM table WHERE status = '$status' ";
				if($result = $db->query( $query )){
					$row = $result->fetch_array();
				}
				
				unlink( UPLOAD_DIR.$row[0] );
				$query = "UPDATE table SET $imageKey = '' WHERE status = '$status' ";
				
				$file = $_FILES['image']['name'];
				$tmp_file = $_FILES['image']['tmp_name'];
				move_uploaded_file($tmp_file, UPLOAD_DIR . $file);
			}	print_r($file);
		}			
	}

I originally had it that way but I did not know how to keep track of which image came from which upload prompt. The radio buttons have a name=image[image0] to grab from. I guess the upload prompt can’t be used that way.

I have been trying to write this code in such a way so that the file from the upload prompt image0 would be inserted into table image0 and upload prompt image1 inserted into table image1 and so on so I can have a way to select the right image to keep, delete, replace or display.

I am trying to write the code this way because I discovered when I delete a file from say upload prompt no. 1 first instead of no. 0 first the files are getting mixed up. The wrong file gets deleted or the wrong file gets displayed and the table becomes inconsistent.

It probably can be done in the one foreach loop but I do a similar thing (in online store CMS’s) separately.

I’m also not sure you need 3 radio buttons. Another option is to have a “Delete” label and 2 radio buttons for ‘yes’ and ‘no’ with ‘no’ checked as default. I then have an <input type=“file”…to upload an image.

If the user uploads an image, then the original is overwritten with the new image regardless of which delete radio button is checked.

I first attempt to delete the record from the database. If the query that deletes the db record returns true, then I delete the image file from disk.

I don’t think doing it all in one loop saves much time. Either way, you still have to make a call to the database to run a query and you still have to run unlink() to delete the file from disk or the move function to move it. Whether these 2 operation are done in 1 loop or separately doesn’t make much difference imho.

to upload multiple images in one go you need something similar to this.

 
foreach($_FILES['image']['name'] as $key => $value) {
      
       $uploadedFile = $_FILES['image']['name'][$key];
 
      if(is_uploaded_file($_FILES['image']['tmp_name'][$key]))   {
             move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadedFile));
 
      }
 
}

I haven’t included any error checking to keep things simple.

Thanks. I will consider your alternative idea, but at the moment I am kind of overwhelmed at understanding this.

I moved the file upload code outside of the foreach loop and ran some tests with print_r() and found a clue.

upload_test/Arrayupload_test/ArrayArray ( [0] => red.jpg [1] => [2] => )

I don’t know why print_r() is showing $file to be an array. It should just be
upload_test/red.jpeg

This might be why move_uploaded_file() can’t move the file. It reads it as

upload_test/ArrayArray ( [0] => red.jpg [1] => [2] => )

instead of

upload_test/red.jpeg

Can anyone show me how to fix this?

if (isset($_POST['submitted']) &&
		($image = $_POST['image'])) {
			
		foreach($image as $imageKey => $imageValue) {
			if ($imageValue == 'replace') {
				$query = "SELECT $imageKey FROM table WHERE status = '$status' ";
				if($result = $db->query( $query )){
					$row = $result->fetch_array();
				}
				
				unlink( UPLOAD_DIR.$row[0] );
				
				$query = "UPDATE table SET $imageKey = '' WHERE status = '$status' ";
			}
		}	
		
		$file = $_FILES['image']['name']; 
		$tmp_file = $_FILES['image']['tmp_name'];
		move_uploaded_file($tmp_file, UPLOAD_DIR . $file);
	
		$z = UPLOAD_DIR . $file;
		print_r($z);
		print_r($file);
			
}