How to Multiple File Upload In Laravel 9?

Feb 11, 2022 . Admin


Hi dev,

This post will give you an example of how to multiple file upload in laravel 9?. this example will help you laravel 9 multiple file upload and display the message validation. I explained simply uploading a multiple file in laravel 9 with validation. In this tutorial, you will learn how to upload multiple files in laravel 9 into the database and storage directory with validation.

In this example, we will create two routes one for get method and another for the post method. we created a simple form with file input. So you have to simple select file and then it will upload in the "uploads" directory of the public folder. So you have to simply follow the below step and get multiple file uploaded in laravel 9 application.

And as well as, how to validate file mime type, size, dimension, etc on laravel controller by using laravel 9 validation rules.

This tutorial will work with Laravel versions 5, 6, 7, and 8. When syntax is different across versions, the different syntax will be demonstrated.

Let's start following example:

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2 : Create Controller

In this step, we will create a new MultipleFileUploadController; in this file, we will add two method index() and store() for render view and store file logic.

Let's create MultipleFileUploadController by following command:

php artisan make:controller MultipleFileUploadController
app/Http/Controllers/MultipleFileUploadController.php
<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class MultipleFileUploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('multipleFileUpload');
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'files' => 'required',
            'files.*' => 'required|mimes:pdf,xlx,csv|max:2048',
        ]);
      
        $files = [];
        if ($request->file('files')){
            foreach($request->file('files') as $key => $file)
            {
                $fileName = time().rand(1,99).'.'.$file->extension();  
                $file->move(public_path('uploads'), $fileName);
                $files[]['name'] = $fileName;
            }
        }
  
        foreach ($files as $key => $file) {
            Image::create($file);
        }
     
        return back()
                ->with('success','You have successfully upload file.');
    }
}
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 routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MultipleUploadImageController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::controller(MultipleUploadImageController::class)->group(function(){
    Route::get('multiple-file-upload', 'index');
    Route::post('multiple-file-upload', 'store')->name('file.store');
});
Step 4: Create Blade File resources/views/multipleFileUpload.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>How to Multiple File Upload In Laravel 9? - Mywebtuts.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
       
    <div class="panel panel-primary">
  
        <div class="panel-heading text-center mt-5">
            <h2>How to Multiple File Upload In Laravel 9? - Mywebtuts.com</h2>
        </div>
  
        <div class="panel-body mt-5">
       
            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-dismissible fade show mb-2" role="alert">
                    {{ $message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
      
            <form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
      
                <div class="mb-3">
                    <label class="form-label" for="inputImage">Select Images:</label>
                    <input 
                        type="file" 
                        name="files[]" 
                        id="inputImage"
                        multiple 
                        class="form-control @error('files') is-invalid @enderror">
      
                    @error('files')
                        <span class="text-danger">{{ $message }}</span>
                    @enderror
                </div>
       
                <div class="mb-3">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
           
            </form>
      
        </div>
    </div>
</div>
</body>
</html>
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/multiple-file-upload

I hope it can help you...

#Laravel 9