public function test2(Request $request) { $this->validate($request,[ ‘name‘ =>‘required‘, ‘content‘ =>‘required‘, ]); if($request->hasFile(‘img_file‘) && $request->file(‘img_file‘)->isValid()){ // 图片 重新命名 $img_name = uniqid(mt_rand(100000,999999)) . ‘.‘ . $request->img_file-> extension(); $foldername=date(‘Y-m-d‘); //文件夹名称 $folderpath="./uploads/{$foldername}"; //文件夹路径 if(!is_dir($folderpath)){ $data = mkdir($folderpath,0777,true); } // 图片地址 $image_file = $folderpath .DIRECTORY_SEPARATOR. $img_name; // 移动图片并生成缩略图 Image::make($request->img_file)->resize(100, null, function($constraint){$constraint->aspectRatio();})->save($image_file); // dump($image);die; // 添加数据表路径 $path = $image_file; }else{ return "<script> alert(‘上传图片不能为空!‘)</script>"; } // 过滤非数据表字段 $data = $request->except(‘_token‘,‘img_file‘); // 添加到数据表 $info = [ ‘title‘ => $data[‘name‘], ‘content‘ => $data[‘myany‘], ‘image_path‘ =>$path, ‘createtime‘ =>time(), ]; $data = Stus::insert($info); // 添加成功跳转 if($data){ return redirect(‘test1‘); } }
效果:
原文:https://www.cnblogs.com/shenchanglu/p/11351865.html