---
description: >-
  https://www.itsolutionstuff.com/post/laravel-9-file-upload-tutorial-exampleexample.html
---

# Laravel 9 File Upload Tutorial Example

Here, I will show you laravel 9 file upload example. This article will give you a simple example of file upload in laravel 9. if you want to see an example of laravel 9 upload file to the database then you are the right place. we will help you to give examples of how to upload and display files in laravel 9. you will do the following things for laravel 9 file upload with preview.

In this tutorial, we will create two routes one for get method to render forms and another for post method to upload file code. we created a simple form with file input. So you have to simply select a file and then it will upload in the "uploads" directory of the public folder.

So you have to simply follow bellow steps and get the file upload in laravel 9 application.

![](https://www.itsolutionstuff.com/upload/laravel-9-file-upload.png)

**Step 1: Install Laravel 9**

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

```
composer create-project laravel/laravel example-app
```

**Step 2: Create Controller**

In this step, we will create a new FileController; in this file, we will add two method index() and store() for render view and store file logic.

Let's create FileController by following command:

```
php artisan make:controller FileController
```

next, let's update the following code to Controller File.

app/Http/Controllers/FileController.php

```
<?php  namespace App\Http\Controllers;   use Illuminate\Http\Request;  class FileController extends Controller{    /**     * Display a listing of the resource.     *     * @return \Illuminate\Http\Response     */    public function index()    {        return view('fileUpload');    }        /**     * Display a listing of the resource.     *     * @return \Illuminate\Http\Response     */    public function store(Request $request)    {        $request->validate([            'file' => 'required|mimes:pdf,xlx,csv|max:2048',        ]);            $fileName = time().'.'.$request->file->extension();               $request->file->move(public_path('uploads'), $fileName);           /*              Write Code Here for            Store $fileName name in DATABASE from HERE         */             return back()            ->with('success','You have successfully upload file.')            ->with('file', $fileName);       }}
```

Store File in Storage Folder

```
$request->file->storeAs('uploads', $fileName);  // storage/app/uploads/file.png
```

Store File in Public Folder

```
$request->file->move(public_path('uploads'), $fileName);  // public/uploads/file.png
```

Store File in S3

```
$request->file->storeAs('uploads', $fileName, 's3');
```

Read Also: [Laravel 9 CRUD Application Tutorial Example](https://www.itsolutionstuff.com/post/laravel-9-crud-application-tutorial-exampleexample.html)

**Step 3: Create and Add Routes**

Furthermore, open routes/web.php file and add the routes to manage GET and POST requests for render view and store file logic.

routes/web.php

```
<?php  use Illuminate\Support\Facades\Route;  use App\Http\Controllers\FileController;  /*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/  Route::get('file-upload', [FileController::class, 'index']);Route::post('file-upload', [FileController::class, 'store'])->name('file.store');
```

**Step 4: Create Blade File**

At last step we need to create fileUpload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.

resources/views/fileUpload.blade.php

```
<!DOCTYPE html><html><head>    <title>Laravel 9 File Upload Example - ItSolutionStuff.com</title>    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"></head>      <body><div class="container">           <div class="panel panel-primary">        <div class="panel-heading">        <h2>Laravel 9 File Upload Example - ItSolutionStuff.com</h2>      </div>        <div class="panel-body">               @if ($message = Session::get('success'))            <div class="alert alert-success alert-block">                <strong>{{ $message }}</strong>            </div>        @endif              <form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">            @csrf              <div class="mb-3">                <label class="form-label" for="inputFile">File:</label>                <input                     type="file"                     name="file"                     id="inputFile"                    class="form-control @error('file') is-invalid @enderror">                  @error('file')                    <span class="text-danger">{{ $message }}</span>                @enderror            </div>               <div class="mb-3">                <button type="submit" class="btn btn-success">Upload</button>            </div>               </form>            </div>    </div></div></body>    </html>
```

**Run Laravel App:**

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

```
php artisan serve
```

Now, Go to your web browser, type the given URL and view the app output:

Read Also: [Laravel 9 Import Export Excel and CSV File Tutorial](https://www.itsolutionstuff.com/post/laravel-9-import-export-excel-and-csv-file-tutorialexample.html)

```
http://localhost:8000/file-upload
```

Output:

[![](https://go.ezodn.com/charity/http/charity-ads.s3.amazonaws.com/charity_ads/1094/234x60.png)](https://go.ezodn.com/ads/charity/proxy?p_id=eaec9c99-9e42-4b10-6777-83b6f5673969\&d_id=77568\&imp_id=1083305800157635\&c_id=1094\&l_id=10016\&url=https%3A%2F%2Ffundraise.pencilsofpromise.org%2Fgive%2F405130%2F%23!%2Fdonation%2Fcheckout%3Fc_src%3Dadsezoic\&ffid=1\&co=VN)

![](https://www.itsolutionstuff.com/upload/laravel-9-file-upload-2.png)

I hope it can help you...
