Suppose you want to validate any type of file in laravel 10 application. That means you want to validate that, users only be able to upload specific extension files, like you can upload only a jpg image file or you can upload a .csv file only etc.

In this tutorial, I will show you, how we can validate any type of file like video mp4 or image excel, or audio. To validate files, laravel 10 has a beautiful File::types() helper. Using this, we can validate any kind of file. In this tutorial, I will validate some files and show you the source code that, and how we can validate it.

laravel-10-csv-file-validation-example

Validate .csv extension file before uploading that, the file type must be csv.

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        Validator::validate($request->all(), [
            'file' => [
                'required',
                File::types(['csv'])
            ]
        ]);
    }
}

 

Validate .pdf extension file before uploading that, the file type must be pdf.

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        Validator::validate($request->all(), [
            'file' => [
                'required',
                File::types(['pdf'])
            ]
        ]);
    }
}

 

Validate .mp3 extension file before uploading that, the file type must be audio.

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        Validator::validate($request->all(), [
            'file' => [
                'required',
                File::types(['mp3'])
            ]
        ]);
    }
}

 

You can pass min max also to validate file size in Laravel 10 like:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        Validator::validate($request->all(), [
            'attachment' => [
                'required',
                File::types(['mp3', 'mp4'])
                    ->min(1024)
                    ->max(12 * 1024),
            ]
        ]);
    }
}

 

Now show the error message in your blade file like:

resources/views/welcome.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Laravel 10 CSV File Validation Example - Laravelia
                </div>
                 <div class="card-body">                    
                    <form 
                        action="{{ route('user.store') }}" 
                        method="post" 
                        enctype="multipart/form-data"
                    >
                    @csrf
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">
                                File
                            </label>
                            <div class="col-sm-9">
                                <input type="file" class="form-control  @error('file') is-invalid @enderror" name="file">
                                @error('file')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label"></label>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-success btn-block text-white">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: Laravel 10 File Or Image Extension Validation Example

 

Conclusion

Now we know excel file validation in laravel. Now we know pdf validation in laravel. Hope this laravel file extension validation tutorial will help you. After completing video file extension validation in laravel, your concept will be clear about how to validate video files in laravel?