三个文件:模型、控制器和视图
1、模型:upload.PHP
- <?php
- namespace appmodels;
- use yiibaseModel;
- class Upload extends Model
- {
- public $file;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['file'], 'file'],
- ];
- }
- }
- ?>
2、控制器:upload方法
- //文件上传功能
- public function actionUpload()
- {
- $model = new Upload();
- if (Yii::$app->request->isPost) {
- model->file = UploadedFile::getInstance(
- if ($model->file && $model->validate()) {
- model->file->saveAs('../uploads/' .
- }
- }
- return this->render('upload', ['model' =>
- }
3、视图:upload.php
- <?php
- use yiiwidgetsActiveForm;
- ?>
- <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
- <?=
- <button>Submit</button>
- <?php ActiveForm::end() ?>