Encrypt File Before Uploading and Download Example in Laravel

Hello Artisan,

In this brand new encrypt file and store and download example tutorial in Laravel, I will show you how to encrypt file and store them in a storage folder and then download it after decrypting this file. Sometimes we need that we want to encrypt the file before storing and then we need to download that file decrypting it.

In this example, I will show you the only logged-in users can upload files to the server, and then only their uploaded file can download with the following above features. I will use soarecostin/file-vault composer package to create this file encryption in Laravel.

This soarecostin/file-vault composer package will automatically register a facade called FileVault. Let's see how to encrypt and decrypt a file in PHP Laravel and download that.



Step 1: Download Laravel

I am going to start from scratch so that you can do it step by step. So download the Laravel project by the following command:

laravel new test-project


Step 2: Create Auth

We are going to create that kind of system that only authenticated users can upload the file. So we need to create auth system. Run below command one after another:

composer require laravel/ui — dev
php artisan ui bootstrap --auth
npm install
npm run dev


Step 3: Install File Encryption Package

The soarecostin/file-vault package allows us to access the FileVault facade, which exposes a few methods for encrypting and decrypting a file. So run the below command to install:

composer require soarecostin/file-vault


Step 4: Create Route

In this step, we need to add some routes. So update the web.php file like:


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;


Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/', function(){return view('welcome');});
Route::post('/home', [HomeController::class, 'store'])->name('file');
Route::get('/files/{filename}', [HomeController::class, 'download'])->name('download');


Step 5: Update Controller

In this step, we need to update the HomeController.php file to download and upload the file system in Laravel by doing file encryption and decryption.


namespace App\Http\Controllers;

use Illuminate\Support\Str;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use SoareCostin\FileVault\Facades\FileVault;

class HomeController extends Controller
    public function __construct()

    public function index()
        $files = Storage::files('files/' . auth()->id());

        return view('home', compact('files'));

    public function store(Request $request)
            'file' => 'required'

        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $filename = Storage::putFile('files/' . auth()->id(), $request->file('file'));
            if ($filename) {
        return redirect()->route('home')->with('message', 'Upload complete');

    public function download($filename)
        if (!Storage::has('files/' . auth()->id() . '/' . $filename)) {

        return response()->streamDownload(function () use ($filename) {
            FileVault::streamDecrypt('files/' . auth()->id() . '/' . $filename);
        }, Str::replaceLast('.enc', '', $filename));


Step 6: Create View

In this final step, we need to create a form to upload files and show user's own uploaded files. To create home.blade.php and update like that:



Recommended: Upload File and Download Example In Laravel


Now you can encrypt files and then download them in the Laravel application. Hope it can help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js