Okay so I've got an android app that an image to my php web service.
I have decoded my base64 string using base64_decode()
. However when I execute an SQL query to insert my data into the database the script inserts this string \x5265736f75726365206964202333
.
What I want to know is how I can modify my code to turn that string into an image and insert it into my PostGreSQL database which has UTF8
encoding and a bytea
data column type to store the image.
PHP script
header('Content-Type: text/plain; charset=UTF-8');
$conn = pg_connect("database_string");
/* GET 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 INTO DATABASE */
$res = pg_query("INSERT INTO records (photo, name, description, latitude, longitude, project) VALUES ('$file', '$s_name', '$s_desc', '$latitude', '$longitude', '$project')");
I know I'm close to my intended solution. I think I am inserting the decoded base64 string and inserting that but missing the final step to convert it to the original image taken by the android app. Apologies for my newness to android dev'ing.