From 9434b1b79a758192cc871092190dce770f6e86a0 Mon Sep 17 00:00:00 2001 From: wurong <1667629529@qq.com> Date: Thu, 6 Jun 2019 14:44:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E4=B8=8A=E4=B8=8D=E7=9F=A5=E9=81=93?= =?UTF-8?q?=E6=80=8E=E4=B9=88=E6=B2=A1=E4=BA=86=E7=9A=84=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=9A=84=E5=AE=89=E5=85=A8=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/web/controller/Account.php | 14 ++++++++------ app/web/controller/Upload.php | 4 ++-- app/web/view/pc/account/alterinfo.html | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/web/controller/Account.php b/app/web/controller/Account.php index 07219d7..25da52f 100644 --- a/app/web/controller/Account.php +++ b/app/web/controller/Account.php @@ -78,13 +78,14 @@ class Account extends Base{ public function upload_picture() { $file=request()->file('file'); $url_load = db('users')->where(['user_id' => $this->user_id ])->field('head')->select(); - if($url_load[0]['head']!='default.png') { - if(is_file(ROOT_PATH . 'public/upload/users/head/' . $url_load[0]['head'])) { - unlink(ROOT_PATH . 'public/upload/users/head/' . $url_load[0]['head']); - } - } - $info = $file->move(ROOT_PATH . 'public/upload/users/head/'); + + $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/upload/users/head/'); if($info) { + if($url_load[0]['head']!='default.png') { + if(is_file(ROOT_PATH . 'public/upload/users/head/' . $url_load[0]['head'])) { + unlink(ROOT_PATH . 'public/upload/users/head/' . $url_load[0]['head']); + } + } $user['head'] = $info->getSaveName(); $user['user_id'] = $this->user_id; db('users')->update($user); @@ -93,6 +94,7 @@ class Account extends Base{ // $path = $info->getExtension(); // $src = ROOT_PATH . 'public/upload/users/head/'.$user['head']; // dump($path); + return json(array('state'=>1,'src'=>$user['head'])); } else { // 上传失败获取错误信息 diff --git a/app/web/controller/Upload.php b/app/web/controller/Upload.php index 6af0766..d5a10a1 100644 --- a/app/web/controller/Upload.php +++ b/app/web/controller/Upload.php @@ -30,7 +30,7 @@ class Upload extends Base{ // 移动到框架应用根目录/public/uploads/ 目录下 if ($file) { - $info = $file->move(ROOT_PATH . 'public/upload/image/'); + $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/upload/image/'); if ($info) { $url = $info->getSaveName(); $this->save_info($url); @@ -63,7 +63,7 @@ class Upload extends Base{ // 移动到框架应用根目录/public/uploads/ 目录下 if ($file) { - $info = $file->move(ROOT_PATH . 'public/upload/image/'); + $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/upload/image/'); if ($info) { $url = $info->getSaveName(); $this->save_info($url); diff --git a/app/web/view/pc/account/alterinfo.html b/app/web/view/pc/account/alterinfo.html index 752276d..91f1eab 100644 --- a/app/web/view/pc/account/alterinfo.html +++ b/app/web/view/pc/account/alterinfo.html @@ -169,9 +169,11 @@ layui.use('upload', function() { // jq('input[name=img]').val(res.path); // img.src = "" + res.path; // console.log(res.src); - $("#img").attr('src', '__STATIC__/../upload/users/head/' + res.src); - if (res.state == 1) + + if (res.state == 1) { + $("#img").attr('src', '__STATIC__/../upload/users/head/' + res.src); layer.msg('上传头像成功!'); + } else layer.msg('上传头像失败!'); }, -- Gitee