In this tutorial you will learn about the Laravel 9 Upload Multiple Files Tutorial and its application with practical example.
In this Laravel 9 Multiple File Upload Example, I’ll show you how to upload multiple image along with validation in laravel 9. In this laravel 9 multiple image upload example, I’ll show you how to validate and upload multiple image into folder and then save it into database. In this tutorial before saving multiple image into database we will validate image and then save it into directory. Before uploading the image we will validate the image. After successfully uploading multiple images into the folder and saving it in database we will display success message on the screen.
Laravel 9 Multiple File Upload Example
In this step by step tutorial I’ll demonstrate how to upload multiple files in laravel 9 with validation. I’ll also show you how to upload multiple pdf, txt, csv, excel, doc files in laravel 9 application. Please follow the steps to upload multiple files in laravel 9 given below:
- Install Laravel 9
- Database Configuration
- Build Model & Migration
- Create Routes
- Build Multi Upload Controller By Artisan Command
- Create Multiple File Upload Form
- Create Directory inside Storage/app/public
- Run Development Server
Install Laravel 9
1 |
composer create-project --prefer-dist laravel/laravel lara9blog |
Configure Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
.env
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara9blog DB_USERNAME=root DB_PASSWORD= |
Create Model & Migration
Now, we have to define table schema for files table. Open terminal and let’s run the following command to generate a migration along with model file to create files table in our database.
1 |
php artisan make:model File -m |
Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('files', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('path'); $table->timestamps(); }); } |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
After, the migration executed successfully the files table will be created in database.
Create Routes
After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 3 4 |
use App\Http\Controllers\MultiFileUploadController; Route::get('files-upload', [MultiFileUploadController::class, 'index']); Route::post('save-multiple-files', [MultiFileUploadController::class, 'store']); |
Create Controller By Artisan Command
Now, lets create a controller for simple file uploading. Create a controller named MultiFileUploadController using command given below –
1 |
php artisan make:controller MultiFileUploadController |
Once the above command executed, it will create a controller file MultiFileUploadController.php in app/Http/Controllers/ directory. Open the MultiFileUploadController.php file and put the following code in it.
app/Http/Controllers/MultiFileUploadController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\File; class MultiFileUploadController extends Controller { public function index() { return view('multiple-files-upload'); } public function store(Request $request) { $validatedData = $request->validate([ 'files' => 'required', 'files.*' => 'mimes:csv,txt,xlx,xls,pdf' ]); if($request->hasfile('files')) { foreach($request->file('files') as $key => $file) { $path = $file->store('public/files'); $name = $file->getClientOriginalName(); $insert[$key]['name'] = $name; $insert[$key]['path'] = $path; } } File::insert($insert); return redirect('files-upload')->with('status', 'Multiple File has been uploaded Successfully'); } } |
Note:- Before uploading any file make sure you have created following directory in the storage/app/public folder called files.
Create Multiple File Upload Form
In this step, we will create view/blade file to generate and display multiple file Upload Form. Lets create a blade file “multiple-files-upload.blade.php” in “resources/views/” directory and put the following code in it respectively.
resources/views/multiple-files-upload.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!DOCTYPE html> <html> <head> <title>Multiple File Upload in Laravel 9</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container mt-5"> @if(session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif <h2 class="text-center">Laravel 9 Multiple File Upload With Validation</h2> <div class="text-center"> <form name="save-multiple-files" method="POST" action="{{ url('save-multiple-files') }}" accept-charset="utf-8" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-md-12"> <div class="form-group"> <input type="file" name="files[]" placeholder="Choose files" multiple > </div> @error('files') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <div class="col-md-12"> <button type="submit" class="btn btn-primary" id="submit">Submit</button> </div> </div> </form> </div> </div> </body> </html> |
Create Directory inside Storage/app/public
Before uploading any file make sure you have created following directory in the storage/app/public folder called files.
1 |
$path = $request->file('file')->store('public/files'); |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/files-upload |