【问题标题】:malformed utf-8 characters possibly incorrectly encoded格式错误的 utf-8 字符可能编码不正确
【发布时间】:2017-02-17 07:24:42
【问题描述】:

我已在 iOS 中将图像编码为 base64 并将其发送到服务器,然后在该字符串上使用 base64_decode()。但它给出了一个错误

格式错误的 utf-8 字符可能编码不正确

我在 laravel 中这样做并使用 php 核心功能;

public function createIdea(Request $request) {
   $filename = str_random(8);
   $imageStr = $request->input('idea_image');
   $decodeImage = base64_decode($imageStr);
   $image = imagecreatefromstring($decodeImage);
     if ($request->hasFile($image)) {
          $filename = $request->file($image)->getClientOriginalName();
          $moveImage = $request->file($image)->move('images', $filename);
     }
   $idea = new Idea();
   $idea->idea_title = $request->input('idea_title');
   $idea->idea_info = $request->input('idea_info');
   $idea->idea_image = "images/".$filename;
   $idea->idea_location = $request->input('idea_location');
   $idea->idea_description = $request->input('idea_description');
   $idea->idea_description = $request->input('selection');
   $idea->user_id = \Auth::id();
   $idea->save();
   $ideaId = $idea->id;
     if ( ! $idea->id ) {
          $statusCode =404;
          response()->json(array('Status:' => 'Idea Creation FAILED'),$statusCode);
    }else{
          $response = $idea->toJson();
          $statusCode =200;
          return response()->json(array('Status:' => 'Idea Created Successfully', 'ideaId' => $ideaId, 'image' => $imageStr),$statusCode);
}

任何帮助将不胜感激。

【问题讨论】:

  • 编码后的字符串如何发送到服务器?
  • 来自移动应用的POST request
  • 但是 post 请求的数据是如何组装的?也许在发送之前已经完成了一些编码。

标签: php laravel encoding utf-8


【解决方案1】:

在以任何语言提供数据之前以 base64 编码 blob 将允许您将 BLOB 数据作为安全字符串传输!

【讨论】:

  • 正是如此。如果您尝试将图像数据作为原始字节发送,并且在标题中说内容是“UTF-8”,那么浏览器会尝试相信您……除了您发送的数据 毕竟不是 UTF-8!因此,标准做法是使用 base64 编码,它将二进制数据转换为 (UTF-8 ...) 字符流,每个字符存储 6 位数据,因此是“base 64”。
猜你喜欢
  • 2019-06-30
  • 2018-02-28
  • 2018-09-01
  • 2018-05-24
  • 2017-11-27
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多