diff --git a/application/common.php b/application/common.php index b4551575..14cd2f30 100644 --- a/application/common.php +++ b/application/common.php @@ -193,52 +193,64 @@ function teacherNames($list = array()) // 上传文件 -function upload($list,$file) +function upload($list,$file,$isSave=false) { // 移动到框架应用根目录/uploads/ 目录下 $info = $file->move($list['serurl']); // 如果上传成功 - if($info){ - // 成功上传后 获取上传信息 - $list['category'] = $info->getExtension(); - $list['url'] = $info->getSaveName(); - $list['newname'] = $info->getFilename(); - $list['hash'] = $info->hash('sha1'); - $list['userid'] = session('userid'); - - // 实例化文件数据模型 - $field = new \app\system\model\Fields; - - // 判断文件是否已经上传,如果已经上传则退出 - if($field->hasHash($list['hash'])){ - $data = array( - 'msg'=>'文件不能重复上传。', - 'val'=>0 - ); - return $data; - } - - //将文件信息保存 - $data = $field->create($list); - - if($data){ - $data = array( - 'msg'=>'上传成功' - ,'val'=>true - ,'url'=>$list['serurl'].$list['url'] - ); - }else{ - $data = array( - 'msg'=>'保存文件信息失败' - ,'val'=>false - ); - } - }else{ - // 上传失败获取错误信息 + if(!$info){ + // 上传失败获取错误信息 $data = array('msg'=>$file->getError(),'val'=>false,'url'=>null); + return $data; } + // 成功上传后 获取上传信息 + $list['category'] = $info->getExtension(); + $list['url'] = $info->getSaveName(); + $list['newname'] = $info->getFilename(); + $list['hash'] = $info->hash('sha1'); + $list['userid'] = session('userid'); + + // 实例化文件数据模型 + $field = new \app\system\model\Fields; + + // 如果需要保存文件 + if($isSave == true){ + // 判断文件是否已经上传,如果已经上传则退出 + if($field->hasHash($list['hash'])){ + $data = array( + 'msg'=>'文件不能重复上传。', + 'val'=>0 + ); + return $data; + } + + //将文件信息保存 + $data = $field->create($list); + + if($data){ + $data = array( + 'msg'=>'上传成功' + ,'val'=>true + ,'url'=>$list['serurl'].$list['url'] + ); + }else{ + $data = array( + 'msg'=>'保存文件信息失败' + ,'val'=>false + ); + } + }else{ + $data = array( + 'msg'=>'上传成功' + ,'val'=>true + ,'url'=>$list['serurl'].$list['url'] + ); + } + + + // 返回信息 return $data; } diff --git a/application/renshi/controller/Index.php b/application/renshi/controller/Index.php index f5a82079..52a3ed2e 100644 --- a/application/renshi/controller/Index.php +++ b/application/renshi/controller/Index.php @@ -385,7 +385,7 @@ class Index extends Base // 获取表单上传文件 $file = request()->file('file'); // 上传文件并返回结果 - $data = upload($list,$file); + $data = upload($list,$file,true); return json($data); } diff --git a/application/renshi/controller/Student.php b/application/renshi/controller/Student.php index c0b89f2e..71242b2d 100644 --- a/application/renshi/controller/Student.php +++ b/application/renshi/controller/Student.php @@ -363,7 +363,7 @@ class Student extends Base // 获取表单上传文件 $file = request()->file('file'); // 上传文件并返回结果 - $data = upload($list,$file); + $data = upload($list,$file,true); return json($data); } diff --git a/application/rongyu/controller/DwRongyu.php b/application/rongyu/controller/DwRongyu.php index 85950566..5b30d425 100644 --- a/application/rongyu/controller/DwRongyu.php +++ b/application/rongyu/controller/DwRongyu.php @@ -171,29 +171,15 @@ class DwRongyu extends Base public function upload() { - // 获取表单上传文件 例如上传了001.jpg + // 获取文件信息 + $list['text'] = $this->request->post('text'); + $list['serurl'] = $this->request->post('serurl'); + + // 获取表单上传文件 $file = request()->file('file'); - // 移动到框架应用根目录/uploads/ 目录下 - $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif,jpeg'])->move('uploads\danweirongyu'); + // 上传文件并返回结果 + $data = upload($list,$file); - - if($info){ - // 成功上传后 获取上传信息 - $list['url'] = $info->getSaveName(); - $list['url'] = str_replace('\\','/',$list['url']); - - - // 如果图片上传成功,则添加荣誉记录 - $data = dwry::create(['url'=>$list['url']]); - $id = $data->id; - - $id ? $data = array('msg'=>'上传成功','val'=>true,'url'=>$list['url'],'id'=>$id) : $data = array('msg'=>'保存文件信息失败','val'=>false,'url'=>null,'id'=>null); - }else{ - // 上传失败获取错误信息 - $data = array('msg'=>$file->getError(),'val'=>false,'url'=>null); - } - - // 返回信息 return json($data); } diff --git a/application/rongyu/view/dw_rongyu/create.html b/application/rongyu/view/dw_rongyu/create.html index 244167bf..c70c3cc0 100644 --- a/application/rongyu/view/dw_rongyu/create.html +++ b/application/rongyu/view/dw_rongyu/create.html @@ -2,11 +2,10 @@ {block name="jscss"}