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
Do you want to encrypt the file or just the file name?
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.
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?
Don't know, you need to try it out! I never encrypted the complete file because I never needed that!
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!
Cool, you can do a lot by just trying it out ;)
Please or to participate in this conversation.