Hey Laravel enthusiastic! Today we will share with you how to upload file in the Laravel 12 application.
We will create two ways in this tutorial: one for the GET method to render forms and another for the POST method to upload file code. We have designed a simple form with a file input and simply select a file and then it will upload in the “uploads” directory of the public folder.
To upload file in Laravel 12 application ,one have to simply follow the steps below ;
Step for Laravel 12 File Upload Example
- Step 1: Install Laravel 12
- Step 2: Create Controller
- Step 3: Create Routes
- Step 4: Create Blade File
- Run Laravel App
Step 1: Install Laravel 12
To created the Laravel app execute the below command:
laravel new example-app
Step 2: Create Controller
In this step, we will create a new `FileController` which will add two methods: `index()` and `store()` to render views and handle file storage logic.
Let’s create the `FileController` by following this command:
php artisan make:controller FileController
Next, let’s update the following code to the Controller file.
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('fileUpload'); } /** * 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:2048', ]); $fileName = time().'.'.$request->file->extension(); $request->file->move(public_path('uploads'), $fileName); /* Write Code Here for Store $fileName name in DATABASE from HERE */ return back()->with('success', 'File uploaded successfully!') ->with('file', $fileName); } }
Store File in Storage Folder
$request->file->storeAs('uploads', $fileName);
// storage/app/uploads/file.png
Store File in Public Folder
$request->file->move(public_path('uploads'), $fileName);
// public/uploads/file.png
Store File in S3
$request->file->storeAs('uploads', $fileName, 's3');
Step 3: Create Routes
Furthermore, open `routes/web.php` file and add the routes to manage GET and POST requests for rendering views and storing file logic.
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');
Step 4: Create Blade File
In the last step, we need to create a `fileUpload.blade.php` file. In this file, we will create a form with a file input button. So, copy below and paste it into that file.
resources/views/fileUpload.blade.php <!DOCTYPE html> <html> <head> <title>Laravel 12 File Upload Example - ItSolutionStuff.com</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> Laravel 12 File Upload Example - ItSolutionStuff.com</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="inputFile">File:</label> <input type="file" name="file" id="inputFile" 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>
Read More Blog Post:
Laravel CRUD Application Example Tutorial
Laravel Email Verification for Beginners (A Kid-Friendly Guide!)
Laravel Password Reset for Beginners
Run Laravel App:
All the steps have been completed, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/file-upload