Skip to content

Laravel 9 File Upload

In this tutorial, you will learn how to upload files in your Laravel 9 application. It’s going to be a simple and easy-to-understand step-by-step guide.

Step 1: Download and install Laravel 9

Let’s create a brand new project with the below command

composer create-project laravel/laravel file-upload-app

Step 2: Create Controller called FileUploadController.php

Let’s create a controller called FileUploadController.php with the artisan make command as shown below.

$ php artisan make:controller FileUploadController

Step 3: Create a Request called FileUploadRequest.php

Let’s create a Request called FileUploadRequest.php with the artisan make command as shown below.

$ php artisan make:request FileUploadRequest

Step 4: Add validation Rules in FileUploadRequest.php

Open your FileUploadRequest.php file and upload validation rules as shown below.


namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FileUploadRequest extends FormRequest
     * Determine if the user is authorized to make this request.
     * @return bool
    public function authorize()
        return true;

     * Get the validation rules that apply to the request.
     * @return array
    public function rules()
        return [
            'file' => 'required|mimes:pdf,xlx,csv|max:2048',

Step 5: Add index and Store methods in FileUploadController.php

Now open your FileUploadController.php file, then, add the index function to load the view file and store the method to handle the upload process.


namespace App\Http\Controllers;

use App\Http\Requests\FileUploadRequest;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;

class FileUploadController extends Controller
    public function index(): Factory|View|Application
        return view('fileUpload');

    public function store(FileUploadRequest $request): RedirectResponse
        $file = $request->file('file');
        $fileName = $file->getClientOriginalName();
        $file->storeAs('uploads', $fileName); // store file with original name
        // $file->store('uploads); // store file with random name
        return back()
            ->with('success', 'File have been upload successfully.');

Step 6: Create a view file called fileUpload.blade.php with the below HTML

Now create a view file called fileUpload.blade.php, with a form to upload the file as shown below.

<!DOCTYPE html>
    <title>Laravel 9 File Upload -</title>
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="container">

    <div class="panel panel-primary">

        <div class="panel-heading text-center mt-5">
            <h2>Laravel 9 File Upload -</h2>

        <div class="panel-body mt-5">

            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-dismissible fade show mb-2" role="alert">
                    {{ $message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

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

                <div class="mb-3">
                    <label class="form-label" for="inputFile">Select File:</label>
                        class="form-control @error('file') is-invalid @enderror">

                        <span class="text-danger">{{ $message }}</span>

                <div class="mb-3">
                    <button type="submit" class="btn btn-primary">Upload</button>



Step 7: Add new routes in the routes/web.php file

Route::get('uploadFile', [FileUploadController::class, 'index']);
Route::post('uploadFile', [FileUploadController::class, 'store'])->name('');

Step 8: Start and test the application.

Now start the application $ php artisan serve and access the application in your browser at http://localhost:8000/uploadFile, you should get simpler output after uploading the file.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments