Laravel Upload Multiple Images and Files with Validation

Published on April 4, 2020 2 min read

We're available to do freelance project. Take a look at our services!

In this article, I’m going to show how to upload multiple images and files with validation. I’m testing in Laravel 7. Let’s get started:

Table of Contents

  1. Create Model, Migration & Controller
  2. Config Model & Migration
  3. Add Methods to Controller
  4. Create View
  5. Define Routes
  6. Run & Test

Create Model, Migration & Controller

Let’s create a model, migration and controller for file upload. Run this command to create all three at once:

php artisan make:model FileUpload -mc

Config Model & Migration

Open FileUpload model from app folder and add this code:


namespace App;

use Illuminate\Database\Eloquent\Model;

class FileUpload extends Model
    protected $fillable = [ 'filename' ];

Open the migration file from database/migrations folder and update up() function like this:

public function up()
    Schema::create('file_uploads', function (Blueprint $table) {

Add Methods to Controller

I’m going to create two methods. One for view and another for upload file. Open the controller from app/Http/Controller and paste this code:


namespace App\Http\Controllers;

use App\FileUpload;
use Illuminate\Http\Request;

class FileUploadController extends Controller
    // show form
    public function index() {
        return view('upload');

    // file upload
    public function upload(Request $request)
        $validator = \Validator::make($request->all(), [
            'files' => 'required'

        $total_files = count($request->file('files'));

        foreach ($request->file('files') as $file) {
            // rename & upload files to uploads folder
            $name = uniqid() . '_' . time(). '.' . $file->getClientOriginalExtension();
            $path = public_path() . '/uploads';
            $file->move($path, $name);

            // store in db
            $fileUpload = new FileUpload();
            $fileUpload->filename = $name;

        return back()->with("success", $total_files . " files uploaded successfully");

Create View

Go to resources/views folder and create a blade file named upload.blade.php. Then paste this code:


    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">File Uploader</div>

                    <div class="card-body">

                        <!-- print success message after file upload  -->
                            <div class="alert alert-success">
                                {{ Session::get('success') }}

                        <form method="post" action="{{ route('upload') }}" enctype="multipart/form-data">
                            {{ csrf_field() }}

                            <div class="form-group">
                                <label for="title">Image/file</label>
                                <input type="file" name="files[]" class="form-control-file" multiple="">
                                    <span class="help-block text-danger">{{ $errors->first('files') }}</span>

                            <div class="text-center">
                                <button class="btn btn-primary">Upload</button>

Define Routes

Open routes/web.php file and define two routes:

Route::get('upload', '[email protected]');
Route::post('upload', '[email protected]')->name('upload');

Run & Test

Our application is ready. Let’s run (php artisan serve) our application and visit upload route and test.

The tutorial is over. You can download this project from GitHub. Thank you. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


8 Replies to “Laravel Upload Multiple Images and Files with Validation”

  1. Hi, do you have a tutorial where I can upload multiple images and also images with some associated text please?

Leave a Reply

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