标签:ext 文件上传 格式不正确 rto code on() jpeg val exp
if ($request->hasFile(‘banner_img‘) && $request->file(‘banner_img‘)->isValid()) {//判断文件是否存在 $file = $request->file(‘banner_img‘); $ext = $file->extension(); $filesize = $_FILES[‘banner_img‘][‘size‘]; $filename = md5(time().rand(100000,999999)).‘.‘.$ext; $store_result = $request->file(‘banner_img‘)->move(‘./upload‘,$filename);//上传文件 if(!in_array(strtoupper($ext),[‘JPG‘,‘JPEG‘,‘PNG‘,‘GIF‘])){ return [ ‘code‘=> 100000, ‘msg‘=> ‘图片格式不正确‘.strtoupper($ext), ‘data‘=>null ]; } if(!$filesize || $filesize>1048576 * 20){ return [ ‘code‘=> 100000, ‘msg‘=> ‘检测到文件大小为0或文件大小超过20M‘, ‘data‘=>null ]; } return response()->json([ ‘code‘ => 0, ‘msg‘ => ‘ok‘, ‘data‘=>‘./upload‘.$filename ]); } if ($request->hasFile(‘banner_img‘) && $request->file(‘banner_img‘)->isValid()) {//判断文件是否存在 $log = $request->file(‘banner_img‘); $arr = explode(‘.‘, $_FILES[‘banner_img‘][‘name‘]); $ext = $arr[count($arr) - 1]; $filename = md5(time().rand(100000,999999)).‘.‘.$ext; $filesize = $_FILES[‘banner_img‘][‘size‘]; if(!in_array(strtoupper($ext),[‘JPG‘,‘JPEG‘,‘PNG‘,‘GIF‘])){ return [ ‘code‘=> 100000, ‘msg‘=> ‘图片格式不正确‘.strtoupper($ext), ‘data‘=>null ]; } if(!$filesize || $filesize>1048576 * 20){ return [ ‘code‘=> 100000, ‘msg‘=> ‘检测到文件大小为0或文件大小超过20M‘, ‘data‘=>null ]; } $store_result = $log->storeAs(‘banner‘,$filename,‘banner_public‘); $output = [ // ‘extension‘ => $extension, ‘store_result‘ => $store_result ]; return [ ‘code‘=> 0, ‘msg‘=> ‘ok‘, ‘data‘=>$store_result ]; }标签:ext 文件上传 格式不正确 rto code on() jpeg val exp
原文地址:https://www.cnblogs.com/zhanghuilong/p/11715850.html