diff --git a/app/web/controller/Account.php b/app/web/controller/Account.php index 07219d79b2ab07c675c0e57cb23ea43e03708486..25da52f5497bca12928e1d26fb08be4f727e5a68 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 6af076649040a0c3e75cbf3aa221653385fd3bbf..d5a10a1a44c23e9bd117e0494ebecd677ebae710 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 752276dfa6f712abef830d9e78d75c722c8d78f3..91f1eabb2d169afc45fb4ac4ba12d0882c9e2548 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('上传头像失败!'); },