Mult file upload. How to replace image?

I made a form that inputs text into a database, title, email, description, four image names, four thumb image names, and a confirmation code called status. This form has four file input prompts.
      
After clicking the submit button the forms (action= preview page) opens a preview page. The preview page processes the images in an array like fashion, limiting file size and type, reducing image size, making a thumb copy and reducing that copy down further, then inputting the image names and other info into a table. The preview page then displays the info and images along with an edit button.

The user can click the edit button to go back. The form now displays three radio buttons next to each file input prompt.
Keep, delete, replace. I coded keep and delete, but I am having trouble with replace. When a radio button is selected there is a value I can use to differentiate which file input prompt or image is being chosen. This allows me to keep or delete the correct image. To replace an image there is a replace radio button, but in addition to this the browse button must be used to select a different image. I have no way to identify which file upload prompt was selected. I am new to php. I need some ongoing help with this form. There is probably more than one way to do this and I am open to suggestions if you can help me get there.

A person in a discussion group suggested I just have two radio buttons, delete yes and delete no. The table would be queried. If an image name was deleted (returned a true value), then the image file would be deleted. The file input prompt would always default to replacing the image if a new image had been browsed and the submit button clicked regardless of the radio button position.
preview-outline.php
kadinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
OK, I looked at the code in the attached file, and posted it here in the code snippet so others can see it easily.  The code snippet has the advantage that it gives us line numbers and a unispace font -- makes program code easier to read.

You can use different submit buttons to identify which file upload prompt might be selected.

Also, take a look at this line of code:
if($_FILES['image']['error'][$number] == UPLOAD_ERR_NO_FILE) {continue;}

That is one of several errors that can occur.  You might want to test for the other errors, too.  File uploads present a great security vulnerability if they are not done properly.


<?php
  
/*Mult file upload. How to replace image?

I made a form that inputs text into a database, title, email, description, four image names, four thumb image names, and a confirmation code called status. This form has four file input prompts. 
	
After clicking the submit button the forms (action= preview page) opens a preview page. The preview page processes the images in an array like fashion, limiting file size and type, reducing image size, making a thumb copy and reducing that copy down further, then inputting the image names and other info into a table. The preview page then displays the info and images along with an edit button.

The user can click the edit button to go back. The form now displays three radio buttons next to each file input prompt.
Keep, delete, replace. I coded keep and delete, but I am having trouble with replace. When a radio button is selected there is a value I can use to differentiate which file input prompt or image is being chosen. This allows me to keep or delete the correct image. To replace an image there is a replace radio button, but in adition to this the browse button must be used to select a different image. I have no way to identify which file upload prompt was selected. I am new to php. I need some ongoing help with this form. There is probably more than one way to do this and I am open to suggestions if you can help me get there.

A person in a discussion group suggested I just have two radio buttons, delete yes and delete no. The table would be queried. If an image name was deleted (returned a true value), then the image file would be deleted. The file input prompt would always default to replacing the image if a new image had been browsed and the submit button clicked regardless of the radio button position.  

 
 The table is

`id` AUTO_INCREMENT,
`image0`
`image1`
`image2`
`status`

Input name='image[image0]' can be directed to table `image0` and so on. This way I know which image is which.
 
<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[]" />
 
 
*/

//Preview page
	#-------------first visit to this page multi file upload------------
if( !isset($_POST['status'])) {
	
	if (isset($_POST['submitted']) ) {
	
		$confirm_code = md5(uniqid(rand()));
			
		foreach ($_FILES['image']['name'] as $number => $file) {
  
			if($_FILES['image']['error'][$number] == UPLOAD_ERR_NO_FILE) {continue;}
	
			$filenm = str_replace(' ', '_', $file);
    
			$udfilenm = $_FILES['image']['tmp_name'][$number];
	
			move_uploaded_file($udfilenm, UPLOAD_DIR . $filenm);
			
			$filename[] = $filenm;
		}
	
		$query = "INSERT INTO table VALUES ('','','','','','$filename[0]','$filename[1]','$filename[2]','','','','','','','$confirm_code')";
		if( !$db->query( $query ) ){
			$errString .= "<p>error with database.</p>";
		}
	}
}

	#---------not first visit------ keep -----------------------------

if (isset($_POST['status'])) {	
	
	$status = $_POST['status'];
	$confirm_code = $status;
	
	if (isset($_POST['submitted']) && 
		($image = $_POST['image'])) {
		
		foreach($image as $imageKey => $imageValue) {
			if ($imageValue == 'keep') {
				$query = "SELECT $imageKey FROM table WHERE  status = '$status' ";
				if($result = $db->query( $query )){
					$row = $result->fetch_array();
				}
			
				$filename[0] = $row['image0'];
				$filename[1] = $row['image1'];
				$filename[2] = $row['image2'];
				
			}
		}	
	}
	
	#------------not first visit--------- delete ----------------------------
		
	if (isset($_POST['submitted']) && 
		($image = $_POST['image'])) {
		
		foreach($image as $imageKey => $imageValue) {
			if ($imageValue == 'delete') {
				$query = "SELECT $imageKey FROM table WHERE  status = '$status' ";
				if($result = $db->query( $query )){
					$row = $result->fetch_array();
				}
			
				print_r($row);
				unlink( UPLOAD_DIR.$row[0] );
				
				$query = "UPDATE table SET $imageKey = '' WHERE status = '$status' ";
				if( !$db->query( $query ) )	$errString .= "<p>error with database.</p>";
			}
		}
		
		$query = "SELECT image0, image1, image2 FROM table WHERE  status = '$status' ";
				if($result = $db->query( $query )){
					$row = $result->fetch_array();
				}
				$filename[0] = $row['image0'];
				$filename[1] = $row['image1'];
				$filename[2] = $row['image2'];
		
	}

	#----------------not first visit--------- replace ---------------------------

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);
		}			
	}
}
?>	


<div>
	<img src= "<?php echo UPLOAD_DIR.$filename[0]; ?>" />
	<img src= "<?php echo UPLOAD_DIR.$filename[1]; ?>" />
	<img src= "<?php echo UPLOAD_DIR.$filename[2]; ?>" />
</div>

<a href="http://www.site.com/post.php?edit=<?php echo $confirm_code; ?>"><button>edit</button></a>

Open in new window

Ray PaseurCommented:
This book might be useful to you: http://www.sitepoint.com/books/phpmysql4/

The code snippet may be helpful with this: "I have no way to identify which file upload prompt was selected."

Please install that and run it, then post back here if you have specific questions.  Best of luck with your project, ~Ray
<?php // RAY_temp_kadin.php
error_reporting(E_ALL);


// IF THERE IS A POST METHOD REQUEST
if (!empty($_POST))
{
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";
}

// END OF PHP - DROP INTO HTML
?>
<form method="post">

<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[]" />
<!-- SEE WHAT THIS SUBMIT BUTTON PUTS INTO THE POST ARRAY -->
<input type="submit" name="submit_0" value="Go" />

<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[]" />
<!-- SEE WHAT THIS SUBMIT BUTTON PUTS INTO THE POST ARRAY -->
<input type="submit" name="submit_1" value="Go" />

<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[]" />
<!-- SEE WHAT THIS SUBMIT BUTTON PUTS INTO THE POST ARRAY -->
<input type="submit" name="submit_2" value="Go" />

</form>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Thanks for the points - it's a good question! ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.