我在数据库中保存文件时遇到问题,我已经完成了编码,但不知何故,编码没有读取我的 File 函数的第一行代码,因为它不断读取 else 行,noimage.jpg如果在提交期间未检测到文件,则该行将保存。我认为问题出在我的刀片上。但我想不出来。选择文件后,文件名也没有出现。数据也保存到我的数据库中,但名称为 noimage.jpg,即使我尝试提交图像。public function store(Request $request) { $this->validate($request,[ 'location' =>'required', 'event_image'=>'image|nullable|max:1999', 'availability'=>'required', ]); //handle the file upload if($request ->hasFile('event_image')){ // to check if user has opted to upload the file // get filename with extention $filenameWithExt = $request->file('event_image')->getClientOriginalName(); //get just filename $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); //get ext (extention) $extension =$request->file('event_image')->getClientOriginalExtension(); //filename to store $fileNameToStore = $filename .'_'.time().'.'.$extension; //upload image $path = $request->file('event_image')->storeAs('public/event_images',$fileNameToStore); }else{ $fileNameToStore ='noimage.jpg'; // if user dont have any file this name will be put on the database } $event = new Event; $event->location = $request->input('event_location'); $event->event_image =$fileNameToStore; $event->availability =$request->input('event_availability'); $event->admin_id = auth()->user()->id; $event->save();}
2 回答
阿波罗的战车
TA贡献1862条经验 获得超6个赞
我认为问题在于html input type file
本质,它name attribute
缺少:
这:
<input type="file" class="custom-file-input" id="customFile"> <label class="custom-file-label" for="customFile" name="event_image" id="event_image"></label>
应该是这样的:
<input type="file" class="custom-file-input" name="event_image" id="customFile"> <label class="custom-file-label" for="customFile" id="event_image"></label>
牧羊人nacy
TA贡献1862条经验 获得超7个赞
您必须用于name
获取表单数据
它应该是
<input type="file" class="custom-file-input" id="customFile" name="event_image ">
- 2 回答
- 0 关注
- 80 浏览
添加回答
举报
0/150
提交
取消