yii下面如何实现文件上传

来源:互联网 发布:淘宝客贷入口在哪儿 编辑:程序博客网 时间:2024/06/05 15:00
控制器::
use yii\data\Pagination;
use yii\web\UploadedFile;
$file = UploadedFile::getInstanceByName('book_img');$name = $file->name;                //获取图片的名称$path = date('Y-m-d',time());       //定义二级目录$rootPath = 'upload/'.$path.'./';  //拼接上传的路径//判断是否有文件    没有则创建if(!file_exists($rootPath)){    mkdir($rootPath,true);}$start = $file->saveAs($rootPath . $name);    //调用模型类中的方法  把文件上传//判断是否上传成功  成功则执行添加
if($start){    $model = new Book();    $model->book_img=$rootPath.$u_file;    $model->attributes = \Yii::$app->request->post();   if($model->insert()){        //执行添加并判断     $model = new Book();
     $model->book_img=$name;     $model->attributes = \Yii::$app->request->post();

 }

0 0
原创粉丝点击