Jump to content

Multi file upload. Go back to edit mode & replace a file.


kadin

Recommended Posts

In this multi file upload form, choose three images, click submit and preview the images on the preview page. If the user wishes to delete or replace an image, click edit and the form will go back to the previous page. Select the replace radio button for example on one of the three images and select a new image from the file input prompt and click submit. The form will go to the preview page again to display the images. During this process the image names are being input into a table and the images are being moved to a directory.

 

The table is

 

  `id`  AUTO_INCREMENT,

  `image0`

  `image1`

  `image2`

  `status`

 

So input name='image[image0]' can be directed to table `image0` and so on.

 

The code for keep and delete work fine, but how do I replace an image?

I have two foreach blocks. The first one deletes the image file from the directory and deletes the image name from the table, but the second foreach dose not move the new image file into 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[]" />

 

<?php
if (isset($_POST['status'])) {	

$status = $_POST['status'];
$confirm_code = $status;

#--------------------------- 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' ";
		}
	}

	foreach($image as $imageKey => $imageValue) {
		if ($imageValue == 'replace') {

			$filenm = $_FILES['image']['name']; 
			$file = $_FILES['image']['tmp_name'];

			move_uploaded_file($file, UPLOAD_DIR . $filenm);
			$filename[] = $filenm; 

			$query = "INSERT INTO table VALUES ('','$filename[0]','$filename[1]','$filename[2]','$confirm_code')";
		}
	}
}	
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.