亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決 Laravel 中保存文件的問題?

如何解決 Laravel 中保存文件的問題?

PHP
繁花不似錦 2024-01-19 16:56:28
我在數據庫中保存文件時遇到問題,我已經完成了編碼,但不知何故,編碼沒有讀取我的 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>


查看完整回答
反對 回復 2024-01-19
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

您必須用于name獲取表單數據

它應該是

<input type="file" class="custom-file-input" id="customFile" name="event_image ">


查看完整回答
反對 回復 2024-01-19
  • 2 回答
  • 0 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號