D a i l y W e b D e s i g n s

Multiple file upload in Laravel 9 with example

This post will demonstrate multiple file uploading using an example from the Laravel 9 tutorial. The Laravel 9 multiple file upload allows us to effortlessly up

This post will demonstrate multiple file uploading using an example from the Laravel 9 tutorial. The Laravel 9 multiple file upload allows us to effortlessly upload several file types such as.pdf,.doc,.docx,.csv,.txt,.png,.gif,.zip, and so on. We’ll add validation rules to Laravel so that only specific file formats are allowed. Now let’s get started.

Step 1: Install Laravel 9

You can skip this step if Laravel 9 is already installed locally on your computer. By entering the following command into your terminal, you can rapidly install the most recent version of Laravel 9. We’ll call it demo-app in this instance, but you may call it anything you want.

				
						composer create-project --prefer-dist laravel/laravel demo-app
				
			

Step 2: Create Migration

				
					cd demo-app
php artisan make:migration create_files_table --create=files

				
			

After running the above command a new migration file will be created in “database/migrations” directory.

database/migrations/2024_08_07_172759_create_files_table.php


				
					<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateFilesTable extends Migration
{
    public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('files');
    }
}
				
			

Now, run the below command to create database tables.

				
					php artisan migrate
				
			

Step 3: Create A Model

This stage involves creating a file model, which enables us to connect to the file table and use the model to execute database operations. To construct a model, execute the command below.

				
					php artisan make:model File
				
			

Then the new file will be created in demo-app/app/Models/File.php. In the model add the name field in the fillable array.

app/Models/File.php


				
					<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class File extends Model
{
    use HasFactory;
    protected $fillable = [
        'name'
    ];
}
				
			

Step 4: Create MultipleFileUploadController





Let’s create a MultipleFileUploadController and let’s add those two methods that we have added in the routes file getFileUploadForm() and store().

app/Http/Controllers/MultipleFileUploadController.php

Notes: To validate .doc, .docx, .xlsx, .xls you need to create mimes.php in config directory and add the following content.

config/mimes.php

				
					<?php
return [
    'doc'  => array('application/msword', 'application/vnd.ms-office'),
    'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
    'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
    'xls' => array('application/vnd.ms-excel'),
];
				
			

Step 5: Create Routes



Now, let’s add the Laravel resource routes in the routes/web.php

				
					<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersMultipleFileUploadController;
Route::get('multiple-file-upload', [ MultipleFileUploadController::class, 'getFileUploadForm' ])->name('get.multipleFileupload');
Route::post('multiple-file-upload', [ MultipleFileUploadController::class, 'store' ])->name('store.multiple-files');
				
			

Step 6: Create Blade/HTML File

Finally, we need to create a file called multiple-file-upload.blade.php in the views folder. We will add the HTML for the multiple-file upload form to this file.

				
					<!DOCTYPE html>
<html>
   <head>
      <title>Laravel 9 Multiple File Upload Tutorial With Example DAILYWEBDESIGN</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
   </head>
   <body>
               <h2>Laravel 9 Multiple File Upload Tutorial With Example - ScratchCode.io</h2>
               @if ($message = Session::get('success'))
                      <button type="button" data-dismiss="alert">×</button>
                      <strong>{{ $message }}</strong>
               @endif
               @if (count($errors) > 0)
                  <strong>Whoops!</strong> There were some problems with your input.
                  <ul>
                     @foreach ($errors->all() as $error)
                     <li>{{ $error }}</li>
                     @endforeach
                  </ul>
               @endif
               <form action="{{ route('store.multiple-files') }}" method="POST" enctype="multipart/form-data">
                  @csrf
                        <label>Select Files:</label>
                        <input type="file" name="documents[]" multiple/>
                        <button type="submit">Upload Files...</button>
               </form>
   </body>
</html>
				
			

Step 7: Output – Laravel 9 Multiple File Upload With Example

Joy! All the instructions for the Laravel 9 multiple files upload tutorial with an example have been finished. Let’s try the command below to see how it functions.

				
					
php artisan serve

				
			

That’s it from our end. We hope this article helped you with the Laravel 9 Multiple File Upload.



Leave a Reply

Your email address will not be published. Required fields are marked *

Daily Web Designs

Email

contact@dailywebdesigns.com

About Us

Hi, I am a web developer passionate about creating and designing beautiful desktop and mobile web interfaces developed in HTML CSS & JavaScript.

Locate Us

©2025. All Rights Reserved .

By Daily Web Designs