title: Inserting multiple image files array into a database
body: <p>I am creating a stock form which includes up to 20 image files. When uploading the images to the server i need to change the name and also insert them into a database so that it will work on the websites carousel. The name changes OK aswell as the upload to the servers folder. The problem I seem to be having is that if I CHOOSE ALL the images it works just fine, but if I CHOOSE less, (i.e; 1, 2, 3, 4 etc) then it breaks. I am probably over looking the obvious. Any help - Any ideas??</p>
  
  <p>Here is where I am;</p>
  
  <pre><code>&lt;?php
  
  if(Input::exists()) {
  
      if(Token::check(Input::get('token'))) {
  
          try {
  
              if(!empty($_FILES['image']['name'][0])) {
  
                  $files = $_FILES['image'];
  
                  $i = 0;
                  $uploaded = array();
  
                  $failed = array();
  
                  $allowed = array('gif', 'png', 'jpg', 'jpeg');
  
                  foreach($files['name'] as $position =&gt; $file_name) {
  
                      $file_tmp = $files['tmp_name'][$position];
                      $file_size = $files['size'][$position];
                      $file_error = $files['error'][$position];
  
                      $file_ext = explode('.', $file_name);
                      $file_ext = strtolower(end($file_ext));
  
                      if(in_array($file_ext, $allowed)) {
  
                          if($file_error === 0) {
  
                              if($file_size &lt;= 2097152) {// 2MB
  
                                  $file_name_new = uniqid('', true) . '.' . $file_ext;
                                  $file_destination = 'assets/uploads/' . $file_name_new;
  
                                  if(move_uploaded_file($file_tmp, $file_destination)) {
                                      $uploaded[$position] = $file_destination;
                                      $image = ($i === 0) ? 'image' : 'image_' . $i;
                                      $i++;
                                  } else {
                                      $failed[$position] = "[{$file_name}] failed to upload";
                                  }
  
                              } else {
                                  $failed[$position] = "[{$file_name}] is too large";
                              }
  
                          } else {
                              $failed[$position] = "[{$file_name}] errored with code [{$file_error}]";
                          }
  
                      } else {
                          $failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed";
                      }
  
                  }
  
              }
  
              $insert = DB::getInstance()-&gt;insert('stock', array(
                      'image'         =&gt;  $uploaded[0],
                      'image_1'       =&gt;  $uploaded[1],
                      'image_2'       =&gt;  $uploaded[2],
                      'image_3'       =&gt;  $uploaded[3],
                      'image_4'       =&gt;  $uploaded[4],
                      'image_5'       =&gt;  $uploaded[5],
                      'image_6'       =&gt;  $uploaded[6],
                      'image_7'       =&gt;  $uploaded[7],
                      'image_8'       =&gt;  $uploaded[8],
                      'image_9'       =&gt;  $uploaded[9],
                      'image_10'      =&gt;  $uploaded[10],
                      'image_11'      =&gt;  $uploaded[11],
                      'image_12'      =&gt;  $uploaded[12],
                      'image_13'      =&gt;  $uploaded[13],
                      'image_14'      =&gt;  $uploaded[14],
                      'image_15'      =&gt;  $uploaded[15],
                      'image_16'      =&gt;  $uploaded[16],
                      'image_17'      =&gt;  $uploaded[17],
                      'image_18'      =&gt;  $uploaded[18],
                      'image_19'      =&gt;  $uploaded[19]
              ));
  
          } catch(Exception $e) {
              die($e-&gt;getMessage());
          }
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>And the Form mockup;</p>
  
  <pre><code>&lt;form action="" method="post" enctype="multipart/form-data"&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-6"&gt;
              &lt;label class="block"&gt;&lt;strong&gt;Upload Vehicle Images&lt;/strong&gt;&lt;br&gt;(Min = 1)&lt;br&gt;(Max = 20)&lt;/label&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="29"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="30"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="31"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="32"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="33"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="34"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="35"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="36"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="37"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="38"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="39"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="40"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="41"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="42"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="43"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="44"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="45"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="46"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="47"&gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="file" name="image[]" id="file" class="form-control" tabindex="48"&gt;&lt;br&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;br&gt;
  &lt;button class="btn btn-primary disabled" type="submit"&gt;FORM SUBMIT&lt;/button&gt;
  &lt;input type="hidden" name="token" value="&lt;?php echo Token::generate(); ?&gt;"&gt;
  </code></pre>
  
  <p></p>
  
  <p>Can anyone help?</p>