android - PHP base64 how to turn into image for insertion into database -


okay i've got android app image php web service.

i have decoded base64 string using base64_decode(). when execute sql query insert data database script inserts string \x5265736f75726365206964202333.

what want know how can modify code turn string image , insert postgresql database has utf8 encoding , bytea data column type store image.

php script

header('content-type: text/plain; charset=utf-8'); $conn = pg_connect("database_string");   /* data */ $name = $_post['name']; $s_name = pg_escape_string($name); $description = $_post['desc'];   $s_desc = pg_escape_string($description); $latitude = $_post['lat']; $longitude = $_post['lng']; $project = $_post['project'];  $encoded_photo = $_post['snap']; $photo = base64_decode($encoded_photo); header('content-type: image/jpeg; charset=utf-8'); $file = fopen('uploaded_image.jpg', 'wb'); fwrite($file, $photo); fclose($file);   /* insert database */ $res = pg_query("insert records (photo, name, description, latitude, longitude, project) values ('$file', '$s_name', '$s_desc', '$latitude', '$longitude', '$project')"); 

i know i'm close intended solution. think inserting decoded base64 string , inserting missing final step convert original image taken android app. apologies newness android dev'ing.

this part:

$file = fopen('uploaded_image.jpg', 'wb'); fwrite($file, $photo); fclose($file);   /* insert database */ $res = pg_query("insert records (photo, name, description, latitude, longitude, project) values ('$file', '$s_name', '$s_desc', '$latitude', '$longitude', '$project')"); 

makes no sense. you're inserting php filehandle. it's being converted string resource id #3 php converted bytea. not want.

it's totally unnecessary write data file in first place, since have in memory. use parameterised queries (you should always use parameterised queries) , let db driver take care of via pg_query_params:

$res = pg_query_params(        'insert records (photo, name, description, latitude, longitude, project) values ($1, $2, $3, $4, %6, $6)',        array( pg_escape_bytea($photo), $s_name, $s_desc, $latitude, $longitude, $project) ); 

see the docs on pg_query_params.

regarding bytea escapes see pg_escape_bytea , pg_unescape_bytea. note these functions pretty behind times , may not recognise hex escapes correctly on output; if have issues, issue sql command set bytea_output = 'escape'; before trying fetch values pg_unescape_bytea.

what php should really doing using fact data type of query's first parameter bytea infer must insert $photo raw bytes rather text. comment sounds not smart enough this.

btw, need read this website understand on reason why parameterized statements should always used. see sql injection , the php manual on sql injection. in addition being more secure, parameterised statements easier right , faster.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -