如何在yii2框架中上传视频

时间:2018-01-22 12:29:07

标签: video yii2

当我尝试上传视频时,我收到以下错误:

  

错误请求(#400)无法验证您的数据提交。

我的控制器代码:

public function actionCreate()
{
    $model = new Videos();

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


      $videoName = "video_".rand();
        $model->video = UploadedFile::getInstance($model,'video');

        if(!empty($model->video)){      
            $model->video->saveAs('../themes/light/images/video/'.$videoName.'.'.$model->video->extension);
            $model->video = 'themes/light/images/video/'.$videoName.'.'.$model->video->extension;
        }
       $model->created_by = Yii::$app->user->identity->id;
        return $this->redirect(['view', 'id' => $model->id]);
    }

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

我的表单:

<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>

<?= $form->field($model, 'video')->fileInput() ?>

<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'date')->textInput(['maxlength' => true]) ?>


<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>

我的模特:

public function rules()
    {
        return [
            [['video', 'title'], 'required'],
            [['videof'], 'file','extensions' => 'mp4','maxSize' => '2048000'],
            [['created_by', 'updated_by'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['video', 'title', 'date'], 'string', 'max' => 255],
        ];
    }

我以前从未编写过上传视频的程序。我不知道是否需要在文件中进行任何配置。请帮帮我。

1 个答案:

答案 0 :(得分:0)

我看到您的代码中有错字,您收到了视频作为文件,但是在规则中,您已将文件设置为 videof

字段