Yii2 kartik 文件 FileInput Multiple

Yii2 kartik file FileInput Multiple

所以我正在使用 Yii2 并且对它还很陌生。我正在使用 Kartik 文件上传并尝试转换多个文件的代码。但它只保存第一个文件。

我已经删除了验证,因为这也失败了,但一旦我知道所有其他方法都正常工作,我会重新添加。

型号:

/**
    * Process upload of image
    *
    * @return mixed the uploaded image instance
    */
    public function uploadImage() {

        // get the uploaded file instance. for multiple file uploads
        // the following data will return an array (you may need to use
        // getInstances method)
         $image = UploadedFile::getInstances($this, 'image');

         foreach ($image as $images) {

        // if no image was uploaded abort the upload
        if (empty($images)) {

            return false;
        }
        // store the source file name
        $this->image_src_filename = $images->name;
        $extvar = (explode(".", $images->name));
        $ext = end($extvar);


        // generate a unique file name
        $this->image_web_filename = Yii::$app->security->generateRandomString().".{$ext}";

        // the uploaded image instance

        return $images;


} }

控制器:

/**
     * Creates a new PhotoPhotos model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new PhotoPhotos();

        if ($model->load(Yii::$app->request->post())) {

            // process uploaded image file instance
            $images = $model->uploadImage();



         if ($model->save(false)) {

            // upload only if valid uploaded file instance found
                if ($images !== false) {

                    $path = $model->getImageFile();
                    $images->saveAs($path);

                }

            return $this->redirect(['view', 'id' => $model->ID]);
        } else {
            //error in saving

        }

    }
            return $this->render('create', [
                'model' => $model,
            ]);
    }

查看:

//uncomment for multiple file upload
echo $form->field($model, 'image[]')->widget(FileInput::classname(), [
    'options'=>['accept'=>'image/*', 'multiple'=>true],
]);

我看到一个问题,就是您在

中颠倒了 $images 和 $image
foreach ($image as $images)

应该是

foreach ($images as $image)

干杯