Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

earthdog's avatar

Proper way to upload/encrypt or decrypt/download a binary file in a laravel form

I want to be able to upload a file through a user firm and then encrypt it with my app key and store it in my servers disk.

Also i need to do the opposite when the user request it.

What is the proper way to do it? I am on L5

0 likes
7 replies
earthdog's avatar

I want to encrypt the contents.

In its simplest form i will use a form filed to upload the file and get it in my controller through Request object.

Before i save it i need to encrypt its contents. I will also encrypt the filename but that is trivial.

earthdog's avatar

This is a code example of mine:

if (Request::hasFile('attachment')) {
            if(Request::file('attachment')->isValid()){
                $attachment = Request::file('attachment');
                $fileContent = File::get($attachment);
                $fileName ='post-'. $newPost->id . '.' . $attachment->getClientOriginalExtension();
                Storage::put($fileName,$fileContent);
            }
        }

You mean that i should use Crypt::encrypt with $fileContent ??

What happens with the encrypted file size?

bobbybouwmann's avatar

Don't know, you need to try it out! I never encrypted the complete file because I never needed that!

earthdog's avatar

I tested it in an xls file and it worked!!

Used this to decrypt:

$file = Storage::get('post-16.xls');
Storage::put('post-16-decrypted.xls',Crypt::decrypt($file));

I will wait a little bit to see if this is the optimal solution for my case.

thanks!

Please or to participate in this conversation.