我在数据库中保存文件时遇到问题,我已经完成了编码,但不知何故,编码没有读取我的 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();
}
阿波罗的战车
牧羊人nacy