一、视图文件代码
<td> <input type="file" name="brand_logo" id="logo" size="45"> <br> <span class="notice-span" style="display:block" id="warn_brandlogo"> 请上传图片,做为品牌的LOGO! </span> </td>
二、控制器代码
use IlluminateSupportFacadesStorage;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
public function add(Request $request) { if($request->isMethod('POST')) { $data = $request->all(); $brand_logo = $request->file('brand_logo'); if($brand_logo->isValid()) { $ext = $brand_logo->getClientOriginalExtension();//后缀 $path = $brand_logo->getRealPath();//路径 $filename = date('Y-m-d-H-i-s').'.'.$ext;//重命名 Storage::disk('upload')->put($filename, file_get_contents($path));//上传 } $data['brand_logo'] = '/laravel56/public/brandpic/'.$filename; $res = DB::table('brand')->insert([ 'brand_name'=>$data['brand_name'], 'brand_logo'=>$data['brand_logo'], 'brand_desc'=>$data['brand_desc'], 'site_url'=>$data['site_url'], 'sort'=>$data['sort'], 'is_show'=>$data['is_show'] ]); if($res) { return redirect('Admin/Brand/brand_list'); } } }
切记在config/filesystems.php中添加如下代码:
'upload' => [ 'driver' => 'local', 'root' => public_path('brandpic'),//brandpic是自己定义的文件保存目录 ],