Our company's ERP software stores employees' pictures as blobs in our MSSQL database.
My task is to find a way to store pictures to the same database table in a way that they can be shown inside the ERP program.
Try #1
$content = file_get_contents('my_path\my_file.png');
INSERT INTO my_table VALUES (CAST('{$data}' AS varbinary(max)))
Produces this error:
SQLSTATE[IMSSP]: An error occurred translating the query string to UTF-16
Checking my encoding:
mb_internal_encoding()
is UTF-8, however
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)
is ISO-8859-1.
Try #2
Converting content encoding to UTF-8:
$content = file_get_contents('my_path\my_file.png');
$data = iconv('', 'UTF-8//TRANSLIT', $content);
INSERT INTO my_table VALUES (CAST('{$data}' AS varbinary(max)))
DOES work for a small black square image.
Trying this on a small red square image however produces this error:
iconv(): Detected an illegal character in input string
Try #3 (a/b/c)
Lots of ways to translate or simply get rid of those illegal characters:
$data = iconv('', 'UTF-8//IGNORE', $content);
$data = base64_encode($data);
$data = mb_convert_encoding($data, 'UTF-8', 'ISO-8859-1');
Allow of them allow us to save to the database, but it doesn't help us because the data are changed and the ERP program can't read them.
Other options
We've thought about the
BulkColumn FROM OPENROWSET(Bulk‘ C: \temp\ nextup.jpg’, SINGLE_BLOB) AS BLOB
way to insert a file into the database, but dismissed it as it requires us to first store the file on the database server's disk, which we don't want to do.
The question
So then the question remains: how to get an uploaded image file into our UTF-16 varbinary field, without modifying the contents?