okokad 2016-07-13 23:41:45 3405次浏览 0条评论 0 0 0

用GII生成后,做以下修改:

控制器:

use yii\web\UploadedFile;

public function actionCreate()
{
        $model = new Product();
        if ($model->load(Yii::$app->request->post()) ) {
            $model->picurl = UploadedFile::getInstance($model, 'picurl');
            if ($model->picurl && $model->validate()) {
                $model->picurl->saveAs('uploadfile/' . $model->picurl->baseName . '.' . $model->picurl->extension);
                $model->save();//保存数据
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                print_r($model->getErrors());
                //echo '校验不成功!';
            }    
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
}

VIEW:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'picurl')->fileInput() ?>
<?php ActiveForm::end(); ?>

模型:

[['picurl'], 'file'],

    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册