本文實(shí)例講述了Laravel框架實(shí)現(xiàn)文件上傳的方法。分享給大家供大家參考,具體如下:
配置文件:
config/filesystems.php,
新建存儲(chǔ)空間
'uplaods' => [ 'driver' => 'local', 'root' => storage_path('app/uploads'), ],
視圖中:
頭像:
input type="file" name="headimg" />
控制器:
$file = $request->file('headimg'); if($file $file->isValid()){ // //獲取原圖片信息 $ext = $file->getClientOriginalExtension(); $originalName = $file->getClientOriginalName(); $type = $file->getClientMimeType(); $path = $file->getRealPath(); //驗(yàn)證圖片類型,大小等 //保存圖片 $save_name = date('YmdHis',time()) .'-' .uniqid() .'.'. $ext; $bool = Storage::disk('uploads')->put($save_name,file_get_contents($path)); if(!$bool){ return redirect()->back()->withErrors('圖片上傳失敗')->withInput(); } }else{ return redirect()->back()->withErrors('請(qǐng)上傳圖片')->withInput(); } //如果驗(yàn)證通過(guò),則繼續(xù)執(zhí)行下面的代碼 $data = $request->input('Student'); //圖片全路徑 $img_web_path = storage_path('app/uploads') . '/' .$save_name; //圖片相對(duì)路徑 $data['headimg'] = $save_name; if(Student::create($data)){ return redirect('Student/index')->with('success','添加成功'); }else{ return redirect()->back(); }
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:迪慶 赤峰 烏魯木齊 鷹潭 山南 濟(jì)寧 三亞 南寧
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel框架實(shí)現(xiàn)文件上傳的方法分析》,本文關(guān)鍵詞 Laravel,框架,實(shí)現(xiàn),文件,上傳,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。