วิธีการ Upload file ภาพ ขึ้น Server
Step 1 - ให้เข้าไปที่ route/web.php เพื่อจะไปตั้งค่า route ไว้สำหรับการ Upload file Step 2 - จากนั้นก็ทำการตั้งค่า Routing แล้วตั้งชื่อ Route ลงไป
ตัวอย่าง Code :
โค้ด: เลือกทั้งหมด
Route::post('/createAirport', 'Controller_show_airport@store');
โค้ด: เลือกทั้งหมด
@extends('layouts.master')
@section('content')
@if ($errors->any()) <!-- ตรวจสอบว่ามี Error ของ validation ขึ้นมาหรือเปล่า -->
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error) <!-- ทำการ วน Loop เพื่อแสดง Error ของ validation ขึ้นมาทั้งหมด -->
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
<!-- ref - https://laravel.com/docs/7.x/validation#DisplayingTheValidationErrors -->
@endif
<div class="table-responsive">
<h2>เพิ่มข้อมูลตารางที่ตั้งสนามบิน</h2>
<form action="/createAirport" method="post" enctype="multipart/form-data"> <!-- enctype="multipart.... ไว้สำหรับการ Upload ข้อมูล -->
{{csrf_field()}}
<div class="form-group">
<label for="name">ชื่อที่ตั้งสนามบิน</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Name">
</div>
<div class="form-group">
<label for="description">ที่อยู่</label>
<input type="text" class="form-control" name="address" id="address" placeholder="Address">
</div>
<div class="form-group">
<label for="type">ละติจูล</label>
<input type="text" class="form-control" name="latitude" id="latitude" placeholder="Latitude">
</div>
<div class="form-group">
<label for="type">ลองติจูล</label>
<input type="text" class="form-control" name="longitude" id="longitude" placeholder="Longitude">
</div>
<div class="form-group">
<label for="image">รูปภาพ</label>
<input type="file" class="form-control" name="image_name" id="image">
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>
</div>
@endsection
Step 12 - Upload File ไปเก็บไว้ใน Folder public/airport_image/ Step 13 - จากนั้นก็ใช้คำสั่งเพิ่มข้อมูลลงไปในฐานข้อมูลพร้อม File ด้วย ตัวอย่าง Code Controller_show_airport.php :
โค้ด: เลือกทั้งหมด
public function store(Request $request)
{
$request->validate([
'name' => 'required|unique:airports',
'address' => 'required',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'image_name' => 'required|file|image|mimes:jpeg,png,jpg|max:5000',
//dd($request->name);
]);
//--------------------- Convert to image -------------------------//
$stringImageReformat=base64_encode('_'.time()); //หากมีการ upload ชื่อ file ซ้ำ ให้ทำการเข้ารหัส base64_encode() ...
//.... เช่น a.jpg = axafaf.jpg พอ upload ซ้ำ ก็จะได้ a.jpge = asfff.jpg ....
//.... มีการกำหนด string '__' แล้วใช้ Function time() ใช้ตรวจสอบ วันที่ Upload file
//dd($stringImageReformat);
$ext = $request->file('image_name')->getClientOriginalExtension(); //ดึงนามสกุล file เข้ามาทำงาน โดยใช้ Function getClientOriginalExtension() ....
//... จะมีการ Return ค่า กลับไปยัง ชื่อ File เดิม ที่ uploade แล้ว ซึ่ง ....
//... ชื่อเดิมก็คือ ค่า empty , ก็จะเหลือแค่ นามสกุล File ที่เอาไว้ใช้งาน
//dd($ext);
$imagename = $stringImageReformat.".".$ext; //ทำการรวมตัวแปร $ext กับ $stringImageReformat
//dd($imagename); //สุ่มเข้ารหัสไปเรื่อยๆ เช่น "XzE1OTA3MDQyMzc=.jpg"
//--------------------- Upload File ใน Local(Your computer) เพื่อ นำ File มาแสดงข้อมูล -------------------------//
//ต้นทาง
$imageEncoded = File::get($request->image_name); //เรียกใช้งาน Module File โดยเอา File ภาพ ไปเก็บไว้ในตัวแปร , ส่วน ....
//... $request รับค่ามาจาก Form view , ส่วน image_name คือ ชื่อ Form view
//ปลายทาง
Storage::disk('local')->put('public/airport_image/'.$imagename,$imageEncoded); //Upload File ไปเก็บไว้ใน Folder ปลายทาง โดยระบุปลายทาง public/Airport_image/
//ref - https://laravel.com/docs/6.x/filesystem#storing-files
//--------------------- Insert -------------------------//
$airport = New Airport;
$airport->name = $request->name;
$airport->address = $request->address;
$airport->lati = $request->latitude;
$airport->longti = $request->longitude;
$airport->image = $imagename; //ไม่ได้จะเอา File จากการ Upload file ไปเก็บไว้ใน Tables ....
//... แต่จะเอาแค่ $imagename ไปเก็บไว้ใน Tables เฉยๆ
$airport->save();
//--------------------- flash message -------------------------//
Session()->flash("success","บันทึกข้อมูลเรียบร้อยแล้ว !"); //สร้าง สถานะ action ไว้สำหรับแจ้งเตือนข้อความ เช่น เพิ่มข้อมูล,แก้ไข,ลบ ...
//... ส่งการแจ้งเตือนไปยัง resources/view/layouts/admin.blade.php
//- ref - https://laravel.com/docs/7.x/session#flash-data
return redirect('/');
}
โค้ด: เลือกทั้งหมด
php artisan storage:link
************************* มี Post ต่อ จาก Post เดิม ****************************