drbii0359 2016-01-06 01:16
浏览 79
已采纳

使用Laravel中的加密文件(如何下载解密文件)

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:

  • id
  • file_path
  • file_name
  • original_name (includes the extension)

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.

  • 写回答

2条回答 默认 最新

  • dongtangjie0495 2016-01-06 01:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退