文件上传的时候应用的类文件找不到 [ 求助交流 ]
文件上传的时候应用的类文件找不到 复制了手册的代码居然报错 哪位大牛可以展示一下MVC层的代码~ 万分致谢!
共 2 条回复
-
model层:
<?php namespace app\models; use Yii; use yii\base\model; use yii\db\ActiveRecord; use yii\web\UploadedFile; class Upload extends Model { public $file; public function rules(){ return [ [['file'],'file','extensions' => 'jpg, png,jpeg', 'mimeTypes' => 'image/jpeg, image/png, image/jpeg','skipOnEmpty' => false], ]; } } ?>
控制器层:
public function actionLoad(){ $upload=new Upload; $request=Yii::$app->request; if($request->isPost){ //echo 'a';die; $upload->file=UploadedFile::getInstance($upload,'file'); //print_r($upload);die; if($upload->file && $upload->validate()){ $upload->file->saveAs('uploads/' . $upload->file->baseName . '.' . $upload->file->extension); }else{ echo '<script>alert("格式不正确")</script>'; die; } } return $this->render('upfile',['model'=>$upload]); }
视图层:
<?php use yii\helpers\Html; use yii\bootstrap\ActiveForm; ?> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'file')->fileInput()->label('选择文件') ?> <button>Submit</button> <?php ActiveForm::end() ?>
共 2 条回复
sevin
注册时间:2016-02-26
最后登录:2016-05-31
在线时长:2小时0分
最后登录:2016-05-31
在线时长:2小时0分
- 粉丝3
- 金钱100
- 威望0
- 积分120