Encrypt files before storing them through Livewire

What seems to be the problem:
How to encrypt files using Livewire before storing them?
Steps to Reproduce:
Current Laravel Code that I want to reproduce using Livewire instead

        // Get File Content
        $fileContent = $file->get();

        // Encrypt the Content
        $encryptedContent = encrypt($fileContent);

Are you using the latest version of Livewire:
Yes
Do you have any screenshots or code examples:

You can add another step in your save() function. Iterate through each of the files, encrypt and overwrite the temporary file and then issue your $file->save('directory') for each file.

public function save()
{
    $this->validate([
        'photos.*' => 'image|max:1024', // 1MB Max
    ]);

    foreach ($this->photos as $photo) {
        $filePath = $photo->getRealPath();
        $fileContents = file_get_contents($filePath);

        $encryptedFileContents = encrypt($fileContents); // encrypt contents
        file_put_contents($filePath, $encryptedFileContents);

        $photo->store('photo');
    }
}
1 Like

Thank you, this works perfectly!

1 Like