0

php script:

$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'];
  }
}

enter image description here

As you can see in the above given picture, I just want to upload the image file name and not its base64 encoded string. How do I do that?

1 Answers1

0

Unfortunalety you can't

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

However, if you have control over the client which is sending the encoded image, you can add the filename in the json payload like this :

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

And get it on your php code :

$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'];
  }
}
mbesson
  • 629
  • 5
  • 24