TP6 上傳圖片到伺服器並返回圖片地址 (框架自帶的上傳)
阿新 • • 發佈:2022-05-09
public function addinfo(){
$info = request()->param();
if(strlen($info['name'])>30){
$this->error('新增失敗,活動名字過長');
}
// print_r($_FILES);die();
if($_FILES['image']['size']>2097152){
$this->error('新增失敗,圖片過大');
}
$fileurl = Images:: fileImg('image',$path='uploads/image');
// print_r($fileurl);die();
$data['image'] = 'https://'.$_SERVER['SERVER_NAME'].'/'.$fileurl['img'];
$data['name'] = $info['name'];
$data['type'] = $info['type'];
$data['money'] = $info['money'];
$data['number'] = $info['number'];
$data['remark'] = $info['remark'];
$data['ctime'] =time();
// print_r($data);exit;
$res = Db::name('activity')->insertGetId($data);
if($res){
//return json(['code'=>'1','msg'=>'新增成功']);
if($info['type'] == 0){
$this->success('新增成功', 'activity/onelist','',1);
}else{
$this->success('新增成功', 'activity/twolist','',1);
}
}else{
//return json(['code'=>'0','msg'=>'新增失敗']);
$this->error('新增失敗');
}
}
/*************************************************
* @zxh
* @DateTime 2021-04-08
* Function: fileImg
* Description: 上傳服務圖片
*************************************************/
static public function fileImg($name,$path='uploads/image')
{
$file = request()->file($name);
//上傳到目錄為public/uploads下
$info = $file->rule('uniqid')->move($path);
// print_r($info);die();
if ($info) {
//獲取到上傳圖片的路徑名稱
$name_img = $path .'/'.$info->getFilename();
$msg = '上傳檔案成功';
} else {
$name_img = '';
$msg = $file->getError();
}
}