多个文件上传在yii2中不起作用

时间:2017-03-22 04:31:48

标签: file-upload yii2 yii2-advanced-app yii-extensions

查看:

 <?php echo FileInput::widget([
'model' => $visaDocument,
'attribute' => 'temp_image[]',
'options' => ['multiple' => true]

]);             ?&GT;

型号:

定义一个变量:

public $temp_image;

在规则中:

[['temp_image'], 'file','extensions' => ' pdf, docx, odt, jpg, png','maxFiles' => 4],

创建了一个函数:

public function upload($model, $id) {

    $imageCount = SbhrmVisaDocument::find()->where(['visa_document_id' => $id])->count();



        foreach ($model->temp_image as $file) {
            if($imageCount < 4){

                $image = new SbhrmVisaDocument();
                $image->save();
                $file->saveAs(\Yii::$app->BasePath . '/uploads/' . $file->name);
                $image->visa_document_name = $file->name;
                $image->emp_id = $id;
                $image->save();
            }
            ++$imageCount;
        }

        return true;

} 

控制器:

  $visaDocument->temp_image = UploadedFile::getInstances($visaDocument, 'temp_image');
        if(!empty($visaDocument->temp_image)){
        if (!$model->upload($visaDocument,$model->emp_id)) {
            return;

       }


      if($model->validate()){
            $model->save();
           //$this->afterSaveSetting($model);
            Yii::$app->session->setFlash('success','Request Submitted');
            return $this->redirect(\yii\helpers\Url::to(['visa-initiation/my-list']));
        }
    }

但是,当我尝试上传多个文件时,只保存了最后一个选定的文件。我只需按照演示http://demos.krajee.com/widget-details/fileinput

1 个答案:

答案 0 :(得分:0)

我认为你应该添加

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

可能会有所帮助。你已经包括了吗?