如何使用表格格式yii2将多个具有相同名称的fileInput字段和多个图像文件上传

时间:2018-08-18 05:50:36

标签: php arrays forms image yii2

我尝试使用表格格式yii2来处理具有相同名称的多个fileInput字段和多个图像文件上传,但这不起作用。

视图

 <?= $form->field($ModelEscortPrices, "[0]duration")->hiddenInput(['value'=> '1'])->label(false)?>
                <label class="control-label">Add Horizontal Images</label>
                <?php echo $form->field($ModelEscortPhoto, '[0]image[]')->widget(FileInput::classname(), [
                    'options' => ['multiple' => true]
                ])->label(false);?>

                <?= $form->field($ModelEscortPrices, "[1]duration")->hiddenInput(['value'=> '2'])->label(false)?>
                <label class="control-label">Add Vertical Images</label>                      
                <?php echo $form->field($ModelEscortPhoto, '[1]image[]')->widget(FileInput::classname(), [
                    'options' => ['multiple' => true],
                ])->label(false);?>

控制器

 $NWModelEscortPhoto = [];
        $photo = Yii::$app->request->post('EscortPhoto', []);
        foreach ($photo as $key => $photos) {
            $NWModelEscortPhoto = new  EscortPhoto();
            $ModelEscortPhoto->image = UploadedFile::getInstances($ModelEscortPhoto, 'image[]');
            $NWModelEscortPhoto->setAttributes($photos);
            $NWModelEscortPhotos[] = $NWModelEscortPhoto;   
            $NWModelEscortPhoto->escort_id=$model->id;  
            $rnd = rand(0,9999);                       
            $fileName = $rnd.$photos->baseName. '.' . $photos->extension;                        
            $NWModelEscortPhoto->image = $fileName;
            if($NWModelEscortPhoto->save()){
                $photos->saveAs(Yii::$app->params['book_image_path'] . $fileName);
            }        
        }

0 个答案:

没有答案