How To Check if Request Has File in Laravel 9 Tutorial

Reading Time: 4 minutes
939 Views

Inside this article we will see the concept i.e How To Check if Request Has File in Laravel 9 Tutorial Tutorial. Article contains the classified information about laravel check request has file uploaded or not?

If you are looking for a solution i.e How to check in Laravel that is uploaded file is present or not then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Article is not for laravel specific version, you can use this concept with any laravel version.

Read More: Laravel Check Array Empty in Blade Examples Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Controller

Open project into terminal and run this command.

$ php artisan make:controller DataController

It will create a DataController.php file inside /app/Http/Controllers folder.

Read More: Laravel Blade Check if Array Key Exists Example Tutorial

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function index()
    {
        return view("form");
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function formSubmit(Request $request)
    {
        $request->validate([
            'photo' => 'required',
        ]);

        if ($request->hasFile('photo')) {

            dd($request->photo);
        }
    }
}

Concept

To check a file is uploaded in form submission:

if ($request->hasFile('photo')) {
            
    dd($request->photo);
}

photo is the name attribute of input type file button.

Create Blade Template File

Create a file form.blade.php inside /resources/views folder.

Open view file and write this code into it.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Check if Request Has File in Laravel 9 Tutorial</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5" style="max-width: 900px">
         
        <div class="alert alert-primary mb-4 text-center">
           <h4>How To Check if Request Has File in Laravel 9 Tutorial</h4>
        </div>  
        <form id="fileUploadForm" method="POST" action="{{ route('file.upload') }}" enctype="multipart/form-data">
            @csrf
            <div class="form-group mb-3">
                <input name="photo" type="file" class="form-control">
            </div>

            <div class="d-grid mt-4">
                <input type="submit" value="Submit" class="btn btn-primary">
            </div>
        </form>
    </div>
</body>
</html>

Create Route

Open web.php file from /routes folder. Add this route into it.

//...

use App\Http\Controllers\DataController;

Route::get('form', [DataController::class, 'index']);
Route::post('form-submit', [DataController::class, 'formSubmit'])->name('file.upload');

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/form

Read More: Laravel Blade Check If Variable Exists or Not Example

Once you select any file and upload, you should see the details of uploaded file as:

We hope this article helped you to learn How To Check if Request Has File in Laravel 9 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more