I am trying to write an image into an SQL Server database in binary format with no success. The image uploaded from my frontend (AngularJS) to my server (PHP) is a Base64
string and I need to then convert it to a raw binary file starting with 0x
as the database requires a varBinary(max)
datatype.
Everything I try seems to end up with the value wrapped in single quotes thus trying to parse it as a varchar and resulting in an error:
General error: 20018 Implicit conversion from data type varchar(max) to varbinary(max) is not allowed.
My current code snippet:
$img = (binary) '0x'.strtoupper(bin2hex($photo['PhotoImage']));
Seems to always result with single quotes and I don't know how to get around it:
PhotoImage='0x6956424F5277304B47676F414141414E53556845556741414156414141414651434141414141444D67336B464'
Update
After some playing around, I have located my problem, I just don't know how to resolve it.
This statement:
$img = (binary)('0x' . bin2hex($photo['PhotoImage']));
dd(gettype($img));
Which (to me) should be returning me a binary value, is in fact STILL returning a string and hence why my S-PROC is failing:
...
"<span class=sf-dump-str title="6 characters">string</span>"
</pre><script>Sfdump("sf-dump-1548954697")</script>
...
Also, as a small test, it seems PHP cannot declare a true binary:
$img = 5;
dd(gettype($img));
/* "<span class=sf-dump-str title="6 characters">integer</span>" */
$img = (binary)5;
dd(gettype($img));
/* "<span class=sf-dump-str title="6 characters">string</span>" */
How can I force PHP to convert this value to a true binary?