How To File Upload In Laravel 10 Example

Websolutionstuff | Mar-15-2023 | Categories : Laravel

In this article, we will see how to file upload in laravel 10 examples. Here, we will learn about the laravel 10 file upload example step by step. Also, we will see how to validate file mime types, size, etc using laravel 10 validation rules.

Also, you can store the file name in the database and display a preview of them. But, here we will simply store the file in the public folder. So, we need to create routes and controllers for that.

So, let's see the laravel 10 file upload example, file upload in laravel 10, how to upload files in laravel 10, and laravel 10 upload files to storage.

Step 1: Install Laravel 10

In this step, we will install laravel 10 using the following code. So, run the following command to the terminal.

composer create-project laravel/laravel laravel_10_file_upload_example

 

Step 2: Add Routes

Now, we will route it into the web.php file.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileUploadController;

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

 

 

Step 3: Create FileUploadController

In this step, we will create a FileUploadController file. So, add the following code to that file.

app/Http/Controllers/FileUploadController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class FileUploadController extends Controller
{
    public function index()
    {
        return view('fileUpload');
    }

    public function store(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,xlx,csv|max:1024',
        ]);
    
        $fileName = time().'.'.$request->file->extension();  
     
        $request->file->move(public_path('uploads'), $fileName);
   
     
        return back()
            ->with('success','File Uploaded successfully.')
            ->with('file', $fileName);
   
    }
}

 

 

Store File in Public Folder:

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

// storage/app/uploads/filename.jpg

Store File in S3:

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

Store File in Storage Folder:

$request->file->move(public_path('uploads'), $fileName);

// public/uploads/filename.jpg

 

Step 4: Create Blade File

In this step, we will create a fileUpload.blade.php file.

resources/views/fileUpload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How To File Upload In Laravel 10 Example - websolutionstuff.com</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
       
    <div class="panel panel-primary">
  
        <div class="panel-heading text-center mt-5">
            <h2>How To File Upload In Laravel 10 Example - websolutionstuff.com</h2>
        </div>
  
        <div class="panel-body mt-5">
       
            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-dismissible fade show mb-2 mt-4" role="alert">
                    {{ $message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
      
            <form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
      
                <div class="mb-3">
                    <label class="form-label" for="inputFile">Select 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>

Output:

how_to_file_upload_in_laravel_10_output

 


You might also like:

Recommended Post
Featured Post
How to Manage Time Zones in React.js Applications
How to Manage Time Zones in Re...

In the interconnected world of web development, where applications are accessed by users spanning multiple time zones, t...

Read More

Sep-04-2023

How to Use JSON Data Field in MySQL Database
How to Use JSON Data Field in...

Today, In this post we will see how to use json field in mysql database. In this tutorial i will give mysql json data ty...

Read More

Jun-04-2021

How To Install TinyMCE Editor In Laravel
How To Install TinyMCE Editor...

In this article, I will give you an example of the TinyMCE editor, Tinymce editor is a rich-text open-source editor,&nbs...

Read More

Jun-18-2020

How to Handle Exception in PHP with Example
How to Handle Exception in PHP...

Hey there, Ever found yourself scratching your head over unexpected errors in your PHP code? Fret not, because today, we...

Read More

Dec-15-2023