文件上传多个错误

时间:2016-09-06 09:47:36

标签: file-upload yii yii2 image-uploading

当我尝试使文件上传多次失败时。错误描述可以看到下面的内容。

  

异常(未知属性)'yii \ base \ UnknownPropertyException',消息'设置未知属性:yii \ validators \ FileValidator :: extension'

此代码控制器

public function actionGallery()
        {
            $model = new \app\models\Gallery();
            if (\Yii::$app->request->post()) {
                $model->image = \yii\web\UploadedFile::getInstances($model, 'image');
                    if ($model->validate()) {
                        foreach ($model->image as $file) {
                            $saveTo = 'uploads/' . $file->baseName . '.' . $file->extension;
                                if ($file->saveAs($saveTo)) {
                                    $model2 = new \app\models\Gallery(['image' => $file->baseName . '.' . $file->extension,
                                        ]);
                                    $model2->save(false);
                                }
                            }
                            Yii::$app->session->setFlash('success', 'Image success uploaded !');
                        }
                    }
                    return $this->render('gallery', ['model' => $model]);
        }

此代码模型

<?php

namespace app\models;

use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "gallery".
 *
 * @property integer $id
 * @property string $image
 */
class Gallery extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gallery';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['image'], 'file', 'extension' => ['png', 'jgp'], ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'image' => 'Image',=
        ];
    }
}

此代码视图

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<h1>Gallery</h1>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'] ]) ?>
    <?= $form->field($model, 'image[]')->fileInput(['multiple' => true]) ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php
ActiveForm::end();  
?>          

0 个答案:

没有答案
相关问题