PHPackages                             initbiz/file-vault - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. initbiz/file-vault

ActiveLibrary[File &amp; Storage](/categories/file-storage)

initbiz/file-vault
==================

2.1.1(10mo ago)01.7k↓50%1MITPHPPHP ^8.0

Since Feb 7Pushed 10mo agoCompare

[ Source](https://github.com/initbiz/file-vault)[ Packagist](https://packagist.org/packages/initbiz/file-vault)[ Docs](https://github.com/initbiz/file-vault)[ RSS](/packages/initbiz-file-vault/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (5)Used By (0)

File encryption / decryption in Laravel
=======================================

[](#file-encryption--decryption-in-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/694e3995c3ed8106577234d28eca2bb42bdfc42245b5dff5b12cb084f7c577eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e697462697a2f66696c652d7661756c742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/initbiz/file-vault)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/f47354ad83765481326ee5a5b936db0a613ba381e1d0bc4c610d84b7d6f7aed7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696e697462697a2f66696c652d7661756c742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/initbiz/file-vault)

With this package, you can encrypt and decrypt files of any size in your Laravel project. This package uses streams and [CBC encryption](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_(CBC)), encrypting / decrypting a segment of data at a time.

Fork
----

[](#fork)

This package is forked from [Brainstud](https://github.com/brainstudnl/file-valut) to add Laravel 10 support.

Installation and usage
----------------------

[](#installation-and-usage)

This package requires PHP 8.0 and Laravel 8.0 or higher.

You can install the package via composer:

```
composer require initbiz/file-vault
```

Usage
-----

[](#usage)

### Tutorials

[](#tutorials)

For a detailed description of how to encrypt files in Laravel using this package, please see the following articles:

- [Part 1: How to encrypt large files in Laravel](https://medium.com/swlh/how-to-encrypt-large-files-in-laravel-293460836ded?source=friends_link&sk=976ab6e5d1cfb52e10c801fe0cb04fca)
- [Part 2: How to encrypt &amp; upload large files to Amazon S3 in Laravel](https://medium.com/@soarecostin/how-to-encrypt-upload-large-files-to-amazon-s3-in-laravel-af88324a9aa?sk=a9a358a3892e898a60448d5314fb3dc0)

### Description

[](#description)

This package will automatically register a facade called `FileVault`. The `FileVault` facade is using the Laravel `Storage` and will allow you to specify a `disk`, just as you would normally do when working with Laravel Storage. All file names/paths that you will have to pass into the package encrypt/decrypt functions are relative to the disk root folder. By default, the `local` disk is used, but you can either specify a different disk each time you call one of `FileVault` methods, or you can set the default disk to something else, by publishing this package's config file.

If you want to change the default `disk` or change the `key`/`cipher` used for encryption, you can publish the config file:

```
php artisan vendor:publish --provider="Initbiz\FileVault\FileVaultServiceProvider"

```

This is the contents of the published file:

```
return [
    /*
     * The default key used for all file encryption / decryption
     * This package will look for a FILE_VAULT_KEY in your env file
     * If no FILE_VAULT_KEY is found, then it will use your Laravel APP_KEY
     */
    'key' => env('FILE_VAULT_KEY', env('APP_KEY')),

    /*
     * The cipher used for encryption.
     * Supported options are AES-128-CBC and AES-256-CBC
     */
    'cipher' => 'AES-256-CBC',

    /*
     * The Storage disk used by default to locate your files.
     */
    'disk' => 'local',
];
```

### Encrypting a file

[](#encrypting-a-file)

The `encrypt` method will search for a file, encrypt it and save it in the same directory, while deleting the original file.

```
public function encrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
```

The `encryptCopy` method will search for a file, encrypt it and save it in the same directory, while preserving the original file.

```
public function encryptCopy(string $sourceFile, string $destFile = null)
```

#### Examples:

[](#examples)

The following example will search for `file.txt` into the `local` disk, save the encrypted file as `file.txt.enc` and delete the original `file.txt`:

```
FileVault::encrypt('file.txt');
```

You can also specify a different `disk`, just as you would normally with the Laravel `Storage` facade:

```
FileVault::disk('s3')->encrypt('file.txt');
```

You can also specify a different name for the encrypted file by passing in a second parameter. The following example will search for `file.txt` into the `local` disk, save the encrypted file as `encrypted.txt` and delete the original `file.txt`:

```
FileVault::encrypt('file.txt', 'encrypted.txt');
```

The following examples both achieve the same results as above, with the only difference that the original file is not deleted:

```
// save the encrypted copy to file.txt.enc
FileVault::encryptCopy('file.txt');

// or save the encrypted copy with a different name
FileVault::encryptCopy('file.txt', 'encrypted.txt');
```

### Decrypting a file

[](#decrypting-a-file)

The `decrypt` method will search for a file, decrypt it and save it in the same directory, while deleting the encrypted file.

```
public function decrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
```

The `decryptCopy` method will search for a file, decrypt it and save it in the same directory, while preserving the encrypted file.

```
public function decryptCopy(string $sourceFile, string $destFile = null)
```

#### Examples:

[](#examples-1)

The following example will search for `file.txt.enc` into the `local` disk, save the decrypted file as `file.txt` and delete the encrypted file `file.txt.enc`:

```
FileVault::decrypt('file.txt.enc');
```

If the file that needs to be decrypted doesn't end with the `.enc` extension, the decrypted file will have the `.dec` extention. The following example will search for `encrypted.txt` into the `local` disk, save the decrypted file as `encrypted.txt.dec` and delete the encrypted file `encrypted.txt`:

```
FileVault::decrypt('encrypted.txt');
```

As with the encryption, you can also specify a different `disk`, just as you would normally with the Laravel `Storage` facade:

```
FileVault::disk('s3')->decrypt('file.txt.enc');
```

You can also specify a different name for the decrypted file by passing in a second parameter. The following example will search for `encrypted.txt` into the `local` disk, save the decrypted file as `decrypted.txt` and delete the original `encrypted.txt`:

```
FileVault::decrypt('encrypted.txt', 'decrypted.txt');
```

The following examples both achive the same results as above, with the only difference that the original (encrypted) file is not deleted:

```
// save the decrypted copy to file.txt while preserving file.txt.enc
FileVault::decryptCopy('file.txt.enc');

// or save the decrypted copy with a different name, while preserving the file.txt.enc
FileVault::decryptCopy('file.txt.enc', 'decrypted.txt');
```

### Streaming a decrypted file

[](#streaming-a-decrypted-file)

Sometimes you will only want to allow users to download the decrypted file, but you don't need to store the actual decrypted file. For this, you can use the `streamDecrypt` function that will decrypt the file and will write it to the `php://output` stream. You can use the Laravel [`streamDownload` method](https://laravel.com/docs/6.x/responses#file-downloads) (available since 5.6) in order to generate a downloadable response:

```
return response()->streamDownload(function () {
    FileVault::streamDecrypt('file.txt')
}, 'laravel-readme.md');
```

### Using a different key for each file

[](#using-a-different-key-for-each-file)

You may need to use different keys to encrypt your files. You can explicitly specify the key used for encryption using the `key` method.

```
FileVault::key($encryptionKey)->encrypt('file.txt');
```

Please note that the encryption key must be 16 bytes long for the `AES-128-CBC` cipher and 32 bytes long for the `AES-256-CBC` cipher.

You can generate a key with the correct length (based on the cipher specified in the config file) by using the `generateKey` method:

```
$encryptionKey = FileVault::generateKey();
```

Testing
-------

[](#testing)

Run the tests with:

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Brainstud](https://github.com/brainstudnl)
- [Costin Soare](https://github.com/soarecostin)
- [Initbiz](https://github.com/initbiz)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

37

—

LowBetter than 82% of packages

Maintenance58

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~514 days

Total

2

Last Release

306d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16684450?v=4)[inIT](/maintainers/initbiz)[@initbiz](https://github.com/initbiz)

---

Top Contributors

[![soarecostin](https://avatars.githubusercontent.com/u/6401790?v=4)](https://github.com/soarecostin "soarecostin (34 commits)")[![niekp](https://avatars.githubusercontent.com/u/19265518?v=4)](https://github.com/niekp "niekp (5 commits)")[![dzapek](https://avatars.githubusercontent.com/u/54842490?v=4)](https://github.com/dzapek "dzapek (4 commits)")[![breadthe](https://avatars.githubusercontent.com/u/17433578?v=4)](https://github.com/breadthe "breadthe (2 commits)")[![jakubwrzalek](https://avatars.githubusercontent.com/u/82502626?v=4)](https://github.com/jakubwrzalek "jakubwrzalek (2 commits)")

---

Tags

phplaravelencryptionencryptdecryptfileCBCdecryptionfile-vault

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/initbiz-file-vault/health.svg)

```
[![Health](https://phpackages.com/badges/initbiz-file-vault/health.svg)](https://phpackages.com/packages/initbiz-file-vault)
```

###  Alternatives

[soarecostin/file-vault

192195.0k](/packages/soarecostin-file-vault)[brainstud/file-vault

1231.6k](/packages/brainstud-file-vault)[betterapp/laravel-db-encrypter

Provides database model attribute encryption/decryption

365614.7k8](/packages/betterapp-laravel-db-encrypter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
