I am reading a BLOB from a SQL data base. The file should contain 1024 float value pairs but I have no clue how to convert them. This is my query:
$stmt = $sqlHandle->query('SELECT convert (varchar (max), convert (varbinary (max), blob)) AS dump FROM data WHERE id = 200
This gets me a String with 32842 chars, which starts like this:
0x000008044072A051E00000004072AD70A00000004072BA8F600000004072C7AE200000004072D4CCC00
I do not know how the blob was created. I only that the first pair should look similar to this after being converted:
248.23112 0.000048741
Can someone explain to me how I get the string converted?
<?php
var_dump(hexdec("0x000008044072A051E00000004072AD70A00000004072BA8F600000004072C7AE200000004072D4CCC00"));
?>
Related
I'm trying to write in a BLOB in my MariaDB using a PHP post call. This BLOB comes from getting a BASE64 from a canvas:
var data= $('#signatureDiv').signature('toDataURL');
var base64data= data.replace(/^data:image\/(png|jpg);base64,/, "");
$.post("myFunc.php", {imgContents: base64data}, function(data) {});
Now, in myFunc.php:
$imgContents = $_POST['imgContents'];
$decodedPNG = base64_decode($imgContents);
$defTest = "UPDATE recogidas SET FirmaProv = ':blobFirma' WHERE ID=$index";
$preparedpdo = $mypdoObject->prepare($defTest);
$preparedpdo->bindParam(':blobFirma', $decodedPNG, PDO::PARAM_LOB);
$preparedpdo->execute();
echo $decodedPNG;
I decode the PNG to save it as a BLOB. Then I prepare a SQL update to upload this BLOB to a proper MEDIUMBLOB column. However, the data is always truncated to few bytes!! Exactly to those bytes:
3A626C6F624669726D61
When it, as a PNG, needs around 100Kb. I tried using this at the beggining of the PHP:
ini_set("odbc.defaultlrl", "1000K");
But it has no effect. To make sure the decode was right, I tried to echo the decoded BLOB, and it returns if correctly.
Why is my database truncating the data to a pack of bytes?
Extra information: It doesn't matter how the picture I make in the canvas is, the data uploaded to the data base is always those same characters.
Why is my database truncating the data to a pack of bytes?
It isn’t. If you had bothered to check what those hex bytes 3A626C6F624669726D61 actually mean, you would have seen that it is :blobFirma. And since you instructed the database to store that string value in your statement, this is absolutely the result that is to be expected.
If you don’t want to store a fixed string value into the column - then remove the quotes around the placeholder from the statement …
PostgreSQL has this datatype called bytea. It is their version of a blob.
In displaying images, png/jpg/gif, the current trend nowadays is to use Data URLs.
My question is how to convert a bytea value into a Base64 Data URL value?
References
bytea - Binary Data Types. PostgreSQL 9.6 Documentation
Data URL - Copy image as data URI by Umar Hansa. Google Developers. May 2015
You may also fetch the complete image data URL from your database, e.g.:
SELECT 'data:image/gif;base64,' || encode(image_data, 'base64') AS image_url
FROM ...
$img_bin = hex2bin(substr($the_bytea, 2));
$url = 'data:image/gif;base64,' . base64_encode($img_bin);
Postgres bytea is a string starting with \x followed by hexadecimal characters. To get the image in binary format, need to strip off the leading \x and convert from hexadecimal characters to binary with hex2bin. Now that you have it in binary, you can base64_encode it.
I have a BLOB column in my MySQL table. When I echo the value of the field using PHP I get
SBτF¶☼╢ñ═∩P╙ _ë ☺K ☺♦ Å☼♂↔☻ ♥
Is there a way for me to convert the binary to a string of 0s and 1s?
This data is NOT an Image and NOT a text, they are small UDP packages of data where the 0s and 1s have significance and I need to load that data and interpret it
I couldn't find a solution to this, so I'm going to reinvent the wheel a bit.
function binDigitsFromString($str) {
$r = '';
for($i=0; $i<strlen($str); $i++) $r .= sprintf("%08b",ord($str[$i]));
return $r;
}
You aren't supposed to enco the blob data directly, without specifying the content type in the header.
For example, if the blob consist of image data for a PNG image, you'd set the header like this:
header("Content-Type: image/png");
echo $myBlob;
and that will render as an image.
This is worth a read: http://www.mysqltutorial.org/php-mysql-blob/
You can Convert BLOB to Char and not to Varchar
You can use CAST function to convert the corresponding value to Character set utf8
I've been trying to store images in mysql database by encoding them in a base64 string then passing them to a php script that in turn stores this string in a blob (I also tried text) filed. What happens is the string is sent to the php script as it is, but when stored in the database, its is stored as a completely different string.
Here is how i do it:
Bitmap image = BitmapFactory.decodeFile("/sdcard/photo2.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteImage = stream.toByteArray();
String s = Base64.encodeToString(byteImage, Base64.NO_WRAP | Base64.URL_SAFE);
And then the string 's' is passed to the php script (correctly as I've checked it) and the script in turn insert it in the database.
This link http://diffchecker.com/kKD4w16C contains how the original encoded string (on the left of the screen) and the string that is stored in the database (on the right of the screen).
Any ideas why this is happening and how to prevent it?
Thanks in advance.
You dont notice, that u just cut on ~20% of string? Seems like there is limit for your DB field. Something like varchar(255) and u try to store 1500 length string.
http://tinyurl.com/c2e7hru - see here. I just copy the end of your DB field value and find it in "original" value.
Also, check your encoding.
I spent almost a day for this , but did not get success.
What i want to do is, i have a binary file "data.dat"
I want to read the file contents and output it in text format in say "data.txt" in php.
I tried unpack function of php, but requires the type to be mentioned as the first argument(May be i am wrong, new to php).
$data = fread($file, 4); // 4 is the byte size of a whole on a 32-bit PC.
$content= unpack("C", $data); //C for unsigned charecter , i for int and so on...
But what if i dont know that at what place , what type of data is stored in the file that i am reading?
This function is restricting me because of the type.
I want something similar to this
$content= unpack("s", $data); //where s can denote to string
Thanks.
PHP does not have a "binary" type. Binary data is stored in strings. If you read binary data from a file, it's already stored as a string. You do not need to convert it into a string.
If the binary data already represents text in some standard encoding, you don't need to do anything as you already have a valid string. If the binary data represents some encoding, you need to know what you need to do with it, we don't know.