How check string is base64 or not in php?
I have a string and want to test using PHP if it's a valid base64 encoded or not. Show
djot 2,9524 gold badges18 silver badges28 bronze badges asked Nov 25, 2010 at 14:29
2 I realise that this is an old topic, but using the strict parameter isn't necessarily going to help. Running base64_decode on a string such as "I am not base 64 encoded" will not return false. If however you try decoding the string with strict and re-encode it with base64_encode, you can compare the result with the original data to determine if it's a valid bas64 encoded value:
answered May 29, 2012 at 10:11
PottyBertPottyBert 1,8021 gold badge13 silver badges14 bronze badges 14 You can use this function:
xdazz 156k36 gold badges240 silver badges268 bronze badges answered Jun 22, 2012 at 10:13
DennaisDennais 4565 silver badges14 bronze badges 4 Just for strings, you could use this function, that checks several base64 properties before returning true:
answered May 22, 2014 at 15:11
merlucinmerlucin 4234 silver badges8 bronze badges 3 This code should work, as the decode function returns FALSE if the string is not valid:
You can read more about the base64_decode function in the documentation. answered Nov 25, 2010 at 14:33
EdoDodoEdoDodo 8,1203 gold badges23 silver badges30 bronze badges 4 I think the only way to do that is to do a 2 This is a really old question, but I found the following approach to be practically bullet proof. It also takes into account those weird strings with invalid characters that would cause an exception when validating.
I got the idea from this page and adapted it to PHP. answered Nov 24, 2015 at 19:45
Lucio MollinedoLucio Mollinedo 2,0451 gold badge28 silver badges27 bronze badges 1 I tried the following:
The following code implements the above steps:
The above code will may return unexpected results. For e.g for the string "json" it will return false. "json" may be a valid base64 encoded string since the number of characters it has is a multiple of 4 and all characters are in the allowed range for base64 encoded strings. It seems we must know the range of allowed characters of the original string and then check if the decoded data has those characters. answered Jul 28, 2016 at 5:39
Nadir LatifNadir Latif 3,6211 gold badge14 silver badges23 bronze badges I write this method is working perfectly on my projects. When you pass the base64 Image to this method, If it valid return true else return false. Let's try and let me know any wrong. I will edit and learn in the feature.
answered Jul 12, 2019 at 10:41
NiroshanNiroshan 3113 silver badges4 bronze badges 0 Alright guys... finally I have found a bullet proof solution for this problem. Use this below function to check if the string is base64 encoded or not -
answered Feb 3, 2020 at 12:26
bilalbilal 1531 silver badge10 bronze badges 1 if u are doing api calls using js for image/file upload to the back end this might help
answered Apr 16, 2020 at 12:59
Old topic, but I've found this function and It's working:
answered May 2, 2013 at 6:10
KlianKlian 1,4865 gold badges21 silver badges29 bronze badges I code a solution to validate images checking the sintaxy
But a safe way is using Intervention
answered Dec 19, 2018 at 14:20
You can just send the string through You can
also use f.i. regular expressions see whether the string contains any characters outside the base64 alphabet, and check whether it contains the right amount of padding at the end ( answered Nov 25, 2010 at 14:32
WimWim 10.9k39 silver badges56 bronze badges base64_decode() should return false if your base64 encoded data is not valid. answered Nov 25, 2010 at 14:32
ScoopScoop 3101 silver badge4 bronze badges i know that i resort a very old question, and i tried all of the methods proposed; i finally end up with this regex that cover almost all of my cases:
basically i check for every character that is not printable (:graph:) is not a space or tab (\s) and is not a unicode letter (all accent ex: èéùìà etc.) i still get false positive with this chars: £§° but i never use them in a string and for me is perfectly fine to invalidate them. I aggregate this check with the function proposed by @merlucin so the result:
answered Apr 21, 2016 at 9:43
m47730m47730 1,8211 gold badge21 silver badges29 bronze badges MOST ANSWERS HERE ARE NOT RELIABLE In fact, there is no reliable answer, as many non-base64-encoded text will be readable as base64-encoded, so there's no default way to know for sure. Further, it's worth noting that base64_decode will decode many invalid strings For exmaple, That said, your most reliable method is, if you control the input, to add an identifier to the string after you encode it that is unique and not base64, and include it along with other checks. It's not bullet-proof, but it's a lot more bullet resistant than any other solution I've seen. For example:
answered Oct 16, 2019 at 17:27
lilHarlilHar 1,5953 gold badges18 silver badges34 bronze badges 3 I have found my solution by accident. For those who use If the invalid base 64 string was Here is the code.
And here is tests code.
Tests result:
answered Oct 29, 2021 at 19:41
veevee 4,1564 gold badges42 silver badges76 bronze badges To validate without errors that someone sends a clipped base64 or that it is not an image, use this function to check the base64 and then if it is really an image
answered Aug 2 at 18:48
I am using this approach. It expects the last 2 characters to be ==
Update: I ended up doing another check if the one above fails base64_decode($buff, true) answered Jan 14, 2014 at 3:52
3 If data is not valid base64 then function base64_decode($string, true) will return FALSE. answered Nov 25, 2010 at 14:32
citrincitrin 6755 silver badges9 bronze badges 1 How do you check if a string is base64 encoded or not in PHP?This is very similar to my own implementation. ... . Shortest ver function is_base64($s) { $decoded = base64_decode($s, true); return preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s) && false !== $decoded && base64_encode($decoded) == $s; }. How do I check if a string is base64 encoded?Encoded data will always have the following characteristic:. The length of a Base64-encoded string is always a multiple of 4.. Only these characters are used by the encryption: “A” to “Z”, “a” to “z”, “0” to “9”, “+” and “/”. Is base64 encoded PHP?PHP | base64_encode() Function. The base64_encode() function is an inbuilt function in PHP which is used to Encodes data with MIME base64. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64. The base64_encoded data takes 33% more space then original data.
What is == in base64?The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.
|