1. 程式人生 > >laravel文件上傳

laravel文件上傳

roo on() not 文件 -i 保存 控制器 inpu lar

一、視圖文件代碼

 <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 Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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是自己定義的文件保存目錄
        ],

laravel文件上傳