Today, I'm excited to walk you through a simple example of how to upload files in Laravel 11. File uploading is a common task in web development, and Laravel makes it incredibly easy with its powerful features.
In this tutorial, we'll cover the basics of handling file uploads, including how to create the necessary routes, controllers, and views. Also, see how to validate files in laravel 11 and how to check file size in laravel 11.
So, let's see the laravel 11 file upload, laravel 11 file type validation, and laravel 11 file size validation.
To begin, we'll install Laravel using the following Composer command.
composer create-project laravel/laravel laravel-11-example
Next, we'll generate the FileController. Within this controller, we'll define two functions. To do so, please execute the following command:
php artisan make:controller FileController
app/Http/Controllers/FileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class FileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
return view('file-upload');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'file' => 'required|mimes:pdf,xlx,csv|max:4096',
]);
$file_name = time().'.'.$request->file->extension();
$request->file->move(public_path('uploads'), $file_name);
/*
Write Code Here for
Store $file_name name in DATABASE from HERE
*/
return back()->with('success', 'File uploaded successfully!')
->with('file', $file_name);
}
}
Note: create an uploads folder in the public folder.
Store File in Public Folder
$request->file->move(public_path('uploads'), $file_name);
// public/uploads/test.csv
Store File in Storage Folder
$request->file->storeAs('uploads', $file_name);
// storage/app/uploads/test.csv
Store File in S3
$request->file->storeAs('uploads', $file_name, 's3');
Next, we'll define routes for displaying and storing images in Laravel 11. To do this, open the web.php
file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;
Route::get('file-upload', [FileController::class, 'index']);
Route::post('file-upload', [FileController::class, 'store'])->name('file.store');
Create a new Blade file named file-upload.blade.php
. This file will contain the HTML markup for the file upload form.
resources/views/file-upload.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to File Upload in Laravel 11 Example - Techsolutionstuff</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdThisnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3"><i class="fa fa-star"></i> How to File Upload in Laravel 11 Example - Techsolutionstuff</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
<form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label" for="input-file">File:</label>
<input
type="file"
name="file"
id="input-file"
class="form-control @error('file') is-invalid @enderror">
@error('file')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
In the last step, execute the following command to start the Laravel development server.
php artisan serve
You might also like: