title: Yii2 Uploading Multiple Files Not Working
body: <p>I managed to try one out but it's not working in my end: <a href="http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html" rel="nofollow">http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html</a></p>
  
  <p>There's no error message and the data are not saved in the database (MongoDB). Here are my code snippets:</p>
  
  <p><em>Contact.php</em> <strong>Model</strong></p>
  
  <pre><code>class Contact extends ActiveRecord
  {
      public $file;
  
      public $attachment;
  
      /**
       * @inheritdoc
       */
      public static function collectionName()
      {
          return ['iaoy', 'contact'];
      }
  
      /**
       * @inheritdoc
       */
      public function attributes()
      {
          return [
              '_id', 'fname','lname','email','phone','address','contact_type','business_name','notes','company_id','date_added','attachment','sort', 'urls'
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['_id', 'fname','lname','email','phone','address','contact_type','business_name','notes','company_id','date_added','attachment','sort', 'urls'], 'safe'],
              [['fname','lname','contact_type','business_name'], 'required'],
              [['attachment'], 'file', 'skipOnEmpty' =&gt; false, 'extensions' =&gt; 'png, jpg', 'maxFiles' =&gt; 10, 'maxSize'=&gt;20*1024*1024,],
              //[['urls'],'string'],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              //'contact_id' =&gt; 'Contact ID',
              '_id' =&gt; 'Contact ID',
              'contact_type' =&gt; 'Contact Type',
              'business_name' =&gt; 'Business Name',
              'fname' =&gt; 'First Name',
              'lname' =&gt; 'Last Name',
              'email' =&gt; 'Email',
              'phone' =&gt; 'Phone',
              'address' =&gt; 'Address',
              'notes' =&gt; 'Notes',
              'attachment' =&gt; 'Attachment',
              'company_id' =&gt; 'Company ID',
          ];
      }
  
      public function upload()
      {
          if ($this-&gt;validate()) { 
              foreach ($this-&gt;attachment as $file) {
                  $file-&gt;saveAs('archive/contact/' . $file-&gt;baseName . '.' . $file-&gt;extension);
              }
              return true;
          } else {
              return false;
          }
      }
  }
  </code></pre>
  
  <p><em>ContactController.php</em> <strong>Controller</strong></p>
  
  <pre><code>public function actionCreate()
  {
      $session = Yii::$app-&gt;session;      
      $model = new Contact();
  
      if($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
      //if (Yii::$app-&gt;request-&gt;isPost) {
          $model-&gt;company_id = new \MongoId($session['company_id']);
  
          date_default_timezone_set('Asia/Manila');
          $model-&gt;date_added = date('d-m-Y', time());
  
          $model-&gt;attachment = UploadedFile::getInstance($model, 'attachment');
          /*if($model-&gt;attachment) {
              foreach ($model-&gt;attachment as $key =&gt; $file) {
                  $file-&gt;saveAs('archive/contact/'. $file-&gt;baseName . '.' . $file-&gt;extension); //Upload files to server
                  $model-&gt;urls .= 'archive/contact/' . $file-&gt;baseName . '.' . $file-&gt;extension.'**'; //Save file names in database- '**' is for separating images
              }
  
              $path = 'archive/contact/' . $model-&gt;attachment-&gt;baseName . '.' . $model-&gt;attachment-&gt;extension;
  
              $count = 0;
              {
                  while(file_exists($path)) {
                      $path = 'archive/contact/' . $model-&gt;attachment-&gt;baseName . '_'.$count.'.' . $model-&gt;attachment-&gt;extension;
                      $count++;
                  }
              }
              $model-&gt;attachment-&gt;saveAs($path);
              $model-&gt;attachment =  $path;
          }*/
  
          $model-&gt;save(); 
  
          if($model-&gt;save()) {
              Yii::$app-&gt;session-&gt;setFlash('success', "Successfully created contact!");
          } else {
              $model-&gt;save();                  
              if($model-&gt;save()) {
                  Yii::$app-&gt;session-&gt;setFlash('success', "Successfully created contact!");
              } else {
                  Yii::$app-&gt;session-&gt;setFlash('error', "Contact creation failed!");
              }
          }
  
          $model-&gt;refresh();
          return $this-&gt;redirect(['index', 'id' =&gt; $session['user_id']]);
  
      } else {
          return $this-&gt;renderAjax('create', [
              'model' =&gt; $model,
          ]);
      }   
  }
  </code></pre>
  
  <p><em>_form.php</em> <strong>View</strong></p>
  
  <pre><code>echo FileInput::widget([
      'model' =&gt; $model,
      'attribute' =&gt; 'attachment[]',
      'name' =&gt; 'attachment[]',
      'options' =&gt; [
          'multiple' =&gt; true,
          'accept' =&gt; 'image/*'
      ],
      'pluginOptions' =&gt; [
          'showCaption' =&gt; false,
          'showRemove' =&gt; false,
          'showUpload' =&gt; false,
          'browseClass' =&gt; 'btn btn-primary btn-block',
          'browseIcon' =&gt; '&lt;i class="glyphicon glyphicon-camera"&gt;&lt;/i&gt; ',
          'browseLabel' =&gt;  'Attach Business Card',
          'allowedFileExtensions' =&gt; ['jpg','gif','png'],
          'overwriteInitial' =&gt; false
      ],
  ]);
  </code></pre>
  
  <p>I'm not sure what seems to be lacking or wrong. Is it because I am using MongoDB or Kartik's widget? Please let me know your thoughts below.</p>