Mult file upload. How to replace image?

Discussion in 'PHP' started by Mitchell, Oct 15, 2010.

  1. #1
    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')";
    			}
    		}
    	}	
    }
    ?>
    PHP:
     
    Mitchell, Oct 15, 2010 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    why use image[image0], image[image1] and not image[0], image[1] that's more easy to understand.

    And i'm not realy getting your problem, please tell us what you wanted to have the script doing? replace images? delete images?
     
    EricBruggema, Oct 21, 2010 IP