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:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Auth::routes();
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.
app\Http\Controllers\HomeController.php
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()
{
$this->middleware('auth');
}
public function index()
{
$files = Storage::files('files/' . auth()->id());
return view('home', compact('files'));
}
public function store(Request $request)
{
$request->validate([
'file' => 'required'
]);
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$filename = Storage::putFile('files/' . auth()->id(), $request->file('file'));
if ($filename) {
FileVault::encrypt($filename);
}
}
return redirect()->route('home')->with('message', 'Upload complete');
}
public function download($filename)
{
if (!Storage::has('files/' . auth()->id() . '/' . $filename)) {
abort(404);
}
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:
resources/views/home.blade.php
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.
#laravel #laravel-8x #file-upload #file-encryption