Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to upload multiple image in php?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 300
    Comment on it

    Hi Reader's,
    Welcome to FindNerd

    Today we are going to discuss how to upload multiple image in php?

    To upload  the multiple files you can follow the process by uploading it using different names for input. The process of uploading can be done simultaneously whose information will be organized automatically in an array.

    You have to take two or more input fields and give common name for all input fields "[]".

    You can see below example for better understanding.

    This is index.php file in which you can create two or more input fields for uploading image in a form.

    <html>
    
      <head>
        <!-- strat css part-->
         <style type="text/css">
                 #width {
                  max-width: 600px;
                  margin: 0 auto;
                  color:green;
                }
                </style>
       <!--end css part-->            
      </head>      
        <body>
         <div  id="width">
              <form action="insert.php" method="post" enctype="multipart/form-data">
              Upload Files:<br>
              <input name="image[]" type="file"/><br /><br />
              <input name="image[]" type="file" /><br /><br />
              <input name="image[]" type="file" /><br /><br />
              <input type="submit" value="submit" name="submit">
    
           </form>
         </div>
    
       </body>
    
    </html>


    Now here is insert.php file in which you can see functionality of function and upload data into target folder.

    <?php
    //define here target path of uploading image folder
      $target_dir = "upload/";
       if(isset($_POST["submit"])) {
          /*get and arrange data from Post */
           function getdata(&$values) {
              $dataArr = array();
              $count = count($values['name']);
              $val_keys = array_keys($values);
               for ($i=0; $i<$count; $i++) {
                //start foreach loop
                foreach ($val_keys as $key) {
                    $dataArr[$i][$key] = $values[$key][$i];
                     }//end foreach loop
               }
                //end for loop
                 return $dataArr;
            }
    
        /*  End of function  */
    
         $GetImage= getdata($_FILES['image']);
    
        /*  Start code for o Uploading images*/
        foreach ($GetImage 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 successfully uploaded.<BR/>";
                } else {
                    echo "Sorry, there was an error uploading your file.<br/>";
                }
            }
        }
        /*  End code for o Uploading images */
    }
     
    ?> 

    I hope this blog will be helpful for uploading multiple image in a form.

 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: