Laravel 5.6 - Multiple File Upload with Validation Example

March 28, 2018 | Category : Laravel 5.6 Laravel PHP

in this article, i will discuss how to upload multiple files or images with validation and store in into database table using Laravel 5.6 application. i will use request for file upload and create new database records for file uploading. here we will also use jquery and bootstrap for add more image input by using add more button.

Few Days ago i posted Ajax Multiple images upload with Laravel 5 application So if you want to do multiple image upload using ajax then you can follow this link : Laravel 5 Ajax Multiple Image Upload with Preview Example.

So here you have to just follow bellow step to create Laravel 5.6 - Multiple File Upload with Validation Example.

Step 1: Download Laravel 5.6

we are going from scratch, so First of all, we will download a new simple copy source code of Laravel App project by typing the some following command.

composer create-project laravel/laravel livelaravelproject --prefer-dist

Step 2: Create Migration and Model

in second step, we will create database migration for files table and also we will create model for files table.

php artisan make:migration create_files_table

Migration:

<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateFormsTable extends Migration

{

public function up()

{

Schema::create('files', function (Blueprint $table) {

$table->increments('id');

$table->string('filenames');

$table->timestamps();

});

}


public function down()

{

Schema::dropIfExists('files');

}

}

php artisan migrate

now we will create File model by using following command:

php artisan make:model File

Step 3: Create Routes

In third step, we will create routes for multiple file upload. so create two route with GET and POST route example.

routes/web.php

Route::get('file','FileController@create');

Route::post('file','FileController@store');

Step 4: Add FileController File

Now we require to add new FileController controller for manage route So let's create FileController with create and store method. Make sure you need to create "files" folder in your public directory.

app/Http/Controllers/FileController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;


class FileController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('create');

}


/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{


$this->validate($request, [

'filenames' => 'required',

'filenames.*' => 'mimes:doc,pdf,docx,zip'

]);


if($request->hasfile('filenames'))

{

foreach($request->file('filenames') as $file)

{

$name=$file->getClientOriginalName();

$file->move(public_path().'/files/', $name);

$data[] = $name;

}

}


$file= new File();

$file->filenames=json_encode($data);

$file->save();


return back()->with('success', 'Data Your files has been successfully added');

}

}

Step 5: Create View File

in this step we need to create create.blade.php file in resources folder. So let's create file:

resources/views/create.blade.php

<html lang="en">

<head>

<title>Laravel 5.6 Multiple File Upload Example</title>

<script src="jquery/1.9.1/jquery.js"></script>

<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">

</head>

<body>


<div class="container lst">


@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Sorry!</strong> There were more problems with your HTML input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


@if(session('success'))

<div class="alert alert-success">

{{ session('success') }}

</div>

@endif


<h3 class="well">Laravel 5.6 Multiple File Upload</h3>

<form method="post" action="{{url('file')}}" enctype="multipart/form-data">

{{csrf_field()}}


<div class="input-group hdtuto control-group lst increment" >

<input type="file" name="filenames[]" class="myfrm form-control">

<div class="input-group-btn">

<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>

</div>

</div>

<div class="clone hide">

<div class="hdtuto control-group lst input-group" style="margin-top:10px">

<input type="file" name="filenames[]" class="myfrm form-control">

<div class="input-group-btn">

<button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>

</div>

</div>

</div>


<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>


</form>

</div>


<script type="text/javascript">

$(document).ready(function() {

$(".btn-success").click(function(){

var lsthmtl = $(".clone").html();

$(".increment").after(lsthmtl);

});

$("body").on("click",".btn-danger",function(){

$(this).parents(".hdtuto control-group lst").remove();

});

});

</script>


</body>

</html>

Now you are ready to run.

I hope you found your best solution...