Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload multiple images/files with multiple input type with single submit button

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 153
    Comment on it

    Create the html for multiple file upload.The form action would go to the "uploads.php" file. 

    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select Files:
        <input name="blogimg[]" type="file"/><br />
    	<input name="blogimg[]" type="file" /><br />
    	<input name="blogimg[]" type="file" /><br />
    	<input name="blogimg[]" type="file" /><br />
        <input type="submit" value="Upload Bulk Images" name="submit">
    </form>

     

    Create a target folder to store files and a php file ie. upload.php.Here the target folder created as 'uploads' under root directory.

    <?php
    $target_dir = "uploads/";
    if(isset($_POST["submit"])) {
    	/*  Start of Re-arangement of Single array coming from Post */
    	function arrngTomultarr(&$values) {
    		$multi_ary = array();
    		$val_count = count($values['name']);
    		$val_keys = array_keys($values);
    
    		for ($i=0; $i<$val_count; $i++) {
    		    foreach ($val_keys as $key) {
    		        $multi_ary[$i][$key] = $values[$key][$i];
    		    }
    		}
    		return $multi_ary;
    	}
    	/*  End of Re-arangement of Single array coming from Post */
     	$multiarr_arrnged = arrngTomultarr($_FILES['blogimg']);
    	/*  Start of Uploading images of multi-array after rearrangement */
    	foreach ($multiarr_arrnged as $result) {  
    		$name = $result['name'];
    		if( !empty($name)){     // Check For Empty Values 
    			$tmprary_name = $result['tmp_name'];
    			$temp = explode(".", $name);
    			$newfilename = uniqid(). '.' . end($temp);
    
    			if (move_uploaded_file($tmprary_name , $target_dir.$newfilename)) {
    				echo "The file ". basename( $name). " has been uploaded.<BR/>";
    			} else {
    				echo "Sorry, there was an error uploading your file.<br/>";
    			}
    		}
    	}
    	/*  End of Uploading images of multiarray after rearrangement */
    }
    

     

    The file names will be unique so there is no need to check file existence  and the empty selection will be discarded.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: