In my webapp, users can upload files. Before being saved and stored, the contents of the file are encrypted using something like this:
Crypt::encrypt(file_get_contents($file->getRealPath()));
I then use the file system that comes with Laravel to move the file
Storage::put($filePath, $encryptedFile);
I have a table to store information about each file with columns such as:
Now I want the user to be able to download this encrypted file. However, I'm having trouble decrypting the file and returning it to the user. In the file downloads response section of the Laravel documentation, it suggests to do this:
return response()->download($pathToFile, $name, $headers);
It wants a file path which is fine, but at which point can I decrypt the file contents so that it is actually readable?
I do seem to be able to do this:
$encryptedContents = Storage::get($fileRecord->file_path);
$decryptedContents = Crypt::decrypt($encryptedContents);
... but I don't know how to return it as a download with a specified file name.
Decrypt Files From PropertiesRight-click on the encrypted file and select Properties. In the General tab, select Advanced. Now, uncheck the Encrypt contents to secure data radio box and click on OK.
Encrypting or Decrypting FilesYou can download an encrypted file to locally read or edit it. The file remains encrypted on the server.
Sometimes the problem might be that Windows is having the wrong software open the file. Try using the file properties to unlock the file. Go into File Explorer, select Advanced, and clear the Encrypt Contents to Secure Data checkbox. Sometimes this will work to get the file decrypted.
You could manually create the response like so:
$encryptedContents = Storage::get($fileRecord->file_path);
$decryptedContents = Crypt::decrypt($encryptedContents);
return response()->make($decryptedContents, 200, array(
'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($decryptedContents),
'Content-Disposition' => 'attachment; filename="' . pathinfo($fileRecord->file_path, PATHINFO_BASENAME) . '"'
));
You can check out the Laravel API for more info on what the parameters of the make
method are. The pathinfo
function is also used to extract the filename from the path so it sends the correct filename with the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With