如何在yii2中上传多个图像

时间:2016-12-06 05:33:02

标签: php yii2 yii2-advanced-app

我想使用yii2中的活动表单在数据库中上传多个图片。我也试过

这是我的观点

<div class="kv-avatar center-block">
        <?= $form->field($photoModel, 'st_image[]')->fileInput(['class' => 'file-loading avatar-1'])->label(false)?>
            </div>
 <div class="kv-avatar center-block">
        <?= $form->field($photoModel, 'st_image[]')->fileInput(['class' => 'file-loading avatar-1'])->label(false)?>
             </div>

控制器

 $photoModel->st_image = UploadedFile::getInstance($photoModel,'st_image');
            foreach( $photoModel->st_image as $key=>$file)
            {
                $img_name = Yii::$app->security->generateRandomString();
                $file->saveAs($path . $img_name . '.' . $file->extension);
                $images = $img_name . '.' . $file->extension;
                $photoModel->user_id = $id;
                $photoModel->st_image = $images;
                $photoModel->save(false);   
                $photoModel = new Photo();
    }

但它给了我错误

  

为foreach()提供的参数无效

任何人都可以帮助我

1 个答案:

答案 0 :(得分:1)

对于多个实例,您应拨打UploadedFile::getInstances()而不是UploadedFile::getInstance()