title: Yii2 - foreach loop to save all files path in database model
body: <p><strong>Greetings,</strong></p>
  
  <p>i'm using a multiple file upload input in Yii2.
  The multiole file input is saving very well the files selected inside a folder called audio.
  The problen is i cannot inside foreach loop save the name and path of each file in the database model Faixas.php inside a field called ficheiro.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public function actionCreate()
  {
      $model = new Faixas();
  
      if (Yii::$app-&gt;request-&gt;isPost) {
          $model-&gt;files = UploadedFile::getInstances($model, 'files');
          if ($model-&gt;upload()) {
              return $this-&gt;render('create', ['model' =&gt; $model]);
          }
      }
  
      return $this-&gt;render('create', ['model' =&gt; $model]);
  }
  </code></pre>
  
  <p><strong>The Model as a function named upload() to save the files in folder, but the part to save inside the model->ficheiro is not working (don't figure it out ?):</strong></p>
  
  <pre><code>public function rules()
  {
      return [
          [['nome', 'ficheiro', 'dummy1', 'dummy2', 'dummy3', 'dummy4', 'dummy5'], 'string', 'max' =&gt; 255],
          [['files'], 'file', 'skipOnEmpty' =&gt; false, 'extensions' =&gt; 'mp3, ogg, webm', 'maxFiles' =&gt; 30],
      ];
  }
  
  public function upload()
  {   
      $model = new Faixas();
      if ($this-&gt;validate()) { 
          foreach ($this-&gt;files as $file) {
                  $file-&gt;saveAs('audio/' . $file-&gt;baseName . '.' . $file-&gt;extension);
  
                  // this part in not working, why ????
                  $filePath = 'audio/'  . $file-&gt;baseName . '.' . $file-&gt;extension;
                  $model-&gt;ficheiro = $filePath;
                  $model-&gt;save();
      }
          return true;
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p><strong>And finally the view, which renders the input:</strong></p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin(['options' =&gt; ['enctype' =&gt; 'multipart/form-data']]) ?&gt;
  
  &lt;?= $form-&gt;field($model, 'files[]')-&gt;fileInput(['multiple' =&gt; true, 'accept' =&gt; 'audio/*']) ?&gt;
  
  &lt;button&gt;Submit&lt;/button&gt;
  </code></pre>
  
  
  
  <p><strong>Any ideas on the way to get it done?
  Many thanks...</strong></p>
  
  <p><strong>The entire Faixas.php model class:</strong></p>
  
  <pre><code>   &lt;?php
  
  namespace app\models;
  
  use Yii;
  
  /**
  * This is the model class for table "faixas".
  *
  * @property integer $id
  * @property string $nome
  * @property string $ficheiro
  * @property string $dummy1
  * @property string $dummy2
  * @property string $dummy3
  * @property string $dummy4
  * @property string $dummy5
  */
  class Faixas extends \yii\db\ActiveRecord
  {
    /**
     * @inheritdoc
     */
   public static function tableName()
  {
      return 'faixas';
  }
  
  public $files;
  
  /**
   * @inheritdoc
   */
  public function rules()
  {
      return [
          [['nome', 'ficheiro', 'dummy1', 'dummy2', 'dummy3', 'dummy4', 'dummy5'], 'string', 'max' =&gt; 255],
          [['files'], 'file', 'skipOnEmpty' =&gt; false, 'extensions' =&gt; 'mp3, ogg, webm', 'maxFiles' =&gt; 30],
      ];
  }
  
  public function upload()
  {   
  
      if ($this-&gt;validate()) { 
          foreach ($this-&gt;files as $file) {
                  $file-&gt;saveAs('audio/' . $file-&gt;baseName . '.' . $file-&gt;extension);
                  $model = new Faixas();
                  // this part in not working, why ????
                  $filePath = 'audio/'  . $file-&gt;baseName . '.' . $file-&gt;extension;
                  $model-&gt;ficheiro = $filePath;
                  $model-&gt;save();
      }
          return true;
      } else {
          return false;
      }
  }
  
  /**
   * @inheritdoc
   */
  public function attributeLabels()
  {
      return [
          'id' =&gt; Yii::t('app', 'ID'),
          'nome' =&gt; Yii::t('app', 'Nome'),
          'ficheiro' =&gt; Yii::t('app', 'Ficheiro'),
          'dummy1' =&gt; Yii::t('app', 'Dummy1'),
          'dummy2' =&gt; Yii::t('app', 'Dummy2'),
          'dummy3' =&gt; Yii::t('app', 'Dummy3'),
          'dummy4' =&gt; Yii::t('app', 'Dummy4'),
          'dummy5' =&gt; Yii::t('app', 'Dummy5'),
      ];
  }
  
  // coloca o Audio na GridView
  public function getAudio()
  {
      return \Yii::$app-&gt;request-&gt;BaseUrl.'/'.$this-&gt;ficheiro;
  }
  </code></pre>
  
  <p>}</p>