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.
Here is a one-liner inspired by @msg's answer:
$extension = explode('/', mime_content_type($base64_encoded_string))[1];
If this is part of a upload form, you can get the information about the files from the $_FILES variable.
If it's a raw field you can decode it and run it through mime_content_type
or equivalent and take a guess.
If you are open to using libraries, you can look into mimey or php-mimetyper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With