如何将我的 base64 图像 url 缩小到图像名称?

How to narrow down my base64 image url to just the image name?

php 脚本:

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

if ($contentType === "application/json") {
  //Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));

  $decoded = json_decode($content, true);

  //If json_decode failed, the JSON is invalid.
  if(! is_array($decoded)) {
      echo "Invalid json";

  } else {
      $image=$decoded['image'];
  }
}

正如你在上面给出的图片中看到的,我只想上传图像文件名而不是它的base64编码字符串。我该怎么做?

可惜你不行

The base64 string doesn't contain the file name, only the contents of the file ()

但是,如果您可以控制发送编码图像的客户端,则可以像这样在 json 有效负载中添加文件名:

{
  "image": "your base64 encoded image",
  "filename": "image file name"
}

并在您的 php 代码中获取它:

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

if ($contentType === "application/json") {
  //Receive the RAW post data.
  $content = trim(file_get_contents("php://input"));

  $decoded = json_decode($content, true);

  //If json_decode failed, the JSON is invalid.
  if(! is_array($decoded)) {
      echo "Invalid json";

  } else {
      $image=$decoded['image'];
      $fileName=$decoded['filename'];
  }
}