laravel upload file code example
Example 1: Upload image laravel 6
<?php
namespace App\Http\Controllers\simple_image_upload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator,Redirect,Response,File;
use App\Photo;
class ImageUploadController extends Controller
{
//
public function index()
{
return view('simple_image_upload.index');
}
public function store(Request $request)
{
request()->validate([
'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($files = $request->file('profile_image')) {
// Define upload path
$destinationPath = public_path('/profile_images/'); // upload path
// Upload Orginal Image
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
$insert['image'] = "$profileImage";
// Save In Database
$imagemodel= new Photo();
$imagemodel->photo_name="$profileImage";
$imagemodel->save();
}
return back()->with('success', 'Image Upload successfully');
}
}
Example 2: laravel download file from s3
$attachment = TicketAttachment::find($id);
$headers = [
'Content-Type' => 'application/jpeg',
'Content-Disposition' => 'attachment; filename="'. $attachment->name .'"',
];
return \Response::make(Storage::disk('s3')->get($attachment->url), 200, $headers);
Example 3: laravel link storage to public
php artisan storage:link
Example 4: laravel store file
$path = $request->file('avatar')->store(
'avatars', 'public'
);
Example 5: laravel file upload
$('form').submit(function(event) {
event.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url: '{{ url('/agents') }}',
type: 'POST',
data: formData,
success: function(result)
{
location.reload();
},
error: function(data)
{
console.log(data);
}
});
});