I'm able to upload an image file and using PHP's file_get_contents put the binary into memory. From there I've attempted to use a basic update or insert query statement to then put that data into a blob. It keeps throwing errors.
In addition I've tried to use the addslashes PHP function as well, and it still doesn't work.
I've tried:
if (is_uploaded_file($_FILES['myFile']['tmp_name'])){
$fileData = file_get_contents($_FILES['myFile']['tmp_name']);
$fileData = unpack("H*hex",$fileData);
$content = "0x" . $fileData['hex'];
}
Then I try to insert into my database and it doesn't work either.
The datatype for my BLOB field is image.
The error I am most often getting is:
mssql_query(): message: Operand type clash: text is incompatible with image (severity 16)
Any help would be appreciated!
Related
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?
I know this question already asked, but I can't solve my problem, so I explained my problem here kindly help me to solve this.
I am getting data form this example URL by using file_get_contens()
$URL1 = 'abcd.com/xxx';
$URL2 = 'abcd.com/yyy';
$URL3 = 'abcd.com/zzz';
$response1 = file_get_contents($URL1);
$response2 = file_get_contents($URL2);
$response3 = file_get_contents($URL3);
And I compressed response data using gzencode because data too long and added prefix for my reference
then i save compressed data to DB
$arrayResponse['URL1'] = '_|_coMpResSed_|_' . gzencode($response1);
$arrayResponse['URL2'] = '_|_coMpResSed_|_' . gzencode($response2);
$arrayResponse['URL3'] = '_|_coMpResSed_|_' . gzencode($response3);
DB details
Storage Engine : InnoDB
Collation : utf8mb4_unicode_ci
Type : longblob or longtext (both i tried)
And I decompress the data by using gzdecode
$temp1 = explode('_|_coMpResSed_|_', $arrayResponse['URL1']);
$temp2 = explode('_|_coMpResSed_|_', $arrayResponse['URL2']);
$temp3 = explode('_|_coMpResSed_|_', $arrayResponse['URL3']);
if (!empty($temp1[1]) && !empty($temp2[1]) && !empty($temp3[1])) {
$arrayResponse['URL1'] = gzdecode($temp1[1]);//working fine
$arrayResponse['URL2'] = gzdecode($temp2[1]);// getting warning
$arrayResponse['URL3'] = gzdecode($temp3[1]);//working fine
}
And I am getting `Warning:
gzdecode(): data erroron line$arrayResponse['URL2'] = gzdecode($temp2[1]);`
Other lines are working fine . I dont know where I am making mistakes. Can any one help me to get this?
Having same problem, I just had a look at Mysql doc:
https://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).
I just change the column data type to VARBINARY and everything's OK.
We are storing entire image files in PostgreSQL, using bytea columns.
In PHP, am trying to open an image file from the bytea field (these stored as hex), then want to manipulate/convert the image using Imagick.
Must the bytestream be converted out of hex to be manageable - in a file-like way - by Imagick? Is there any other secret sauce?
I wouldn't be surprised if we had to read beyond the file header bits, either. Offending snippet is below:
// Decode image from hex?
$image = new Imagick ($row['thewholefile']);
// ERROR: Uncaught exception 'ImagickException' with message 'Unable to read the file: /x0000000 (etc)
Actually the problem here had to do with PostgreSQL's bytea presentation format- as we are using v9.n of PG, the default output is hex:
We had to first set output to PG's 'Old School' bytea handling. Then, by pg-unescaping the raw column data, we had something we could work with:
SET bytea_output = 'escape'
$unescaped = pg_unescape_bytea($content);
You need readimageblob. Here you go:
http://www.php.net/manual/en/function.imagick-readimageblob.php
The error is pretty clear, just as the documentation: the constructor requires a filename or an array of filenames. Use Jauzsika's solution.
I have little pdf (or txt, odt, doc) file to be store in Postgres DB with php.
I use this php functions to read file:
$fp = fopen($name, 'rb');
$content = fread($fp, filesize($name));
$content = addslashes($content);
fclose($fp);
and then try to store in db:
$sql = "insert into Table(column) values ('$content'::bytea)";
$result = #pg_query($sql);
"column" is bytea type.
When I execute the script with pdf file, I get the follow error:
ERROR: invalid byte sequence for encoding "UTF8": 0xe2e3cf HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
When I execute the script with doc file, I get the follow error:
ERROR: invalid input syntax for type bytea
When I execute the script with txt file, NO error:
What's wrong and what is the proper way to store files?
Have you tried using pg_escape_string instead of addslashes? http://www.php.net/manual/en/function.pg-escape-string.php
How do I write and read string (~200 KB) with lots of 0 bytes inside into MySQL BLOB field?
This is not working:
$data = ... // PNG image raw data
$queryPart = "'" . addslashes($data) . "'";
... compose and execute query
The proper way to escape input data when using the legacy MySQL library is mysql_real_escape_string().
Even if they "can", databases are not made to stock binary files. It's more efficient to have in your table the path of the files on your server.
Use mysql_real_escape_string(), as it makes a call to the MySQL library to clean the given data.
http://php.net/manual/en/function.mysql-real-escape-string.php