【问题标题】:Laravel: encrypt the uploaded file/imageLaravel:加密上传的文件/图像
【发布时间】:2019-11-10 21:51:12
【问题描述】:

我是 laravel 新手,尝试加密上传的文件。这是我的控制器:

if ($file != null && !empty($file)) 
{
   $userfile = DNEUser::find($lastUserId);
   $user_store_pic = $request->file('user_store_pic');
   $fileContent = $user_store_pic->get();
   $encryptedContent = encrypt($fileContent);
   $s3 = \Storage::disk('uploads');
   //$array=explode(" ",$encryptedContent); 
   $user_store_pic_name = $lastUserId.'_'.time().'.' .$encryptedContent->getClientOriginalExtension();
   $filePath = 'store/'.$user_store_pic_name;
   $s3->put($filePath, file_get_contents($encryptedContent));
   $userStorePicName = $filePath;
   $userfile->user_store_pic = $userStorePicName;
   $userfile->save();
}

我正在尝试按照https://stefanzweifel.io/posts/how-to-encrypt-file-uploads-with-laravel/ 加密文件

但我在提交表单时遇到错误:

"ymfony\Component\Debug\Exception\FatalThrowableError (E_ERROR) 调用成员函数 getClientOriginalExtension() on 字符串”

我尝试使用explode转换为数组,但它显示数组的相同错误:

“调用数组上的成员函数getClientOriginalExtension()”

【问题讨论】:

    标签: laravel encryption laravel-encryption


    【解决方案1】:

    您必须使用$user_store_pic->getClientOriginalExtension(); 代替$encryptedContent->getClientOriginalExtension()

    也许对你有帮助。

    【讨论】:

    • 我已经尝试,其示出了ErrorException(E_WARNING), “的file_get_contents(eyJpdiI6Im5qVHI0TVBTNEtWSHorbXY0RVB4eHc9PSIsInZhbHVlIjoiZTk1ZmpSUjVENmlBMlJ5TWsxa3hFQnNPdnpzTmdLeVU5RWQySGNZdkVLRGs2bzRId3dZT2ZiVDdGTFBjTlZNcm▶”跨度>
    • 尝试使用这个:``` $file = $request->file('attachment'); // 获取文件内容 $fileContent = $file->get(); // 加密内容 $encryptedContent = encrypt($fileContent); // 存储加密内容 $s3->put($filePath, $encryptedContent); ```
    • 仍然收到同样的错误:“调用字符串上的成员函数 getClientOriginalExtension()”
    • 在使用此“$user_store_pic->getClientOriginalExtension()”后,您是否收到错误“调用字符串上的成员函数 getClientOriginalExtension()”?
    • @SagarParikhSGR 您在调试代码方面做得很差。如何在使用它们之前弄清楚变量是什么。这样你就不会假设变量内容是什么。 Laravel 包含一个不错的辅助函数调用 dd()
    【解决方案2】:

    替换这个

    if ($file != null && !empty($file)) 
    {
       $userfile = DNEUser::find($lastUserId);
       $user_store_pic = $request->file('user_store_pic');
       $fileContent = $user_store_pic->get();
       $encryptedContent = encrypt($fileContent);
       $s3 = \Storage::disk('uploads');
       //$array=explode(" ",$encryptedContent); 
       $user_store_pic_name = $lastUserId.'_'.time().'.' .$user_store_pic->getClientOriginalExtension();
       $filePath = 'store/'.$user_store_pic_name;
       $s3->put($filePath, $encryptedContent);
       $userStorePicName = $filePath;
       $userfile->user_store_pic = $userStorePicName;
       $userfile->save();
    }
    

    【讨论】:

    • 它显示 ErrorException (E_WARNING),“file_get_contents(eyJpdiI6Im5qVHI0TVBTNEtWSHorbXY0RVB4eHc9PSIsInZhbHVlIjoiZTk1ZmpSUjVENmlBMlJ5TWsxa3hFQnNPdnpzTmdLeVU5RWQySGNZdkVLRGs2dGTFBjTld2kLRGs2dGTFBjTld2)
    • 删除 file_get_contents
    • jsut 替换这一行 $s3->put($filePath,$encryptedContent);
    • 例外:不允许序列化“Illuminate\Http\UploadedFile”
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多