Search code examples
phpbase64file-extension

Get the file extension of a base64 encoded string


How can you get the file extension of a base64 encoded string in PHP?

In my case, this file happens to be an image:

$base64_encoded_string = $_POST['image_base64_string'];

$extension = ??

How can I get the file extension from $base64_encoded_string ?

EDIT: This is NOT part of an upload form so $_FILES data cannot be used here.


Solution

  • Here is a one-liner inspired by @msg's answer:

    $extension = explode('/', mime_content_type($base64_encoded_string))[1];