好吧,所以我有一個Android應用程序,圖像到我的PHP Web服務。PHP的base64如何變成圖像插入數據庫
我使用base64_decode()
解碼了我的base64字符串。但是,當我執行SQL查詢將數據插入數據庫時,腳本會插入此字符串\x5265736f75726365206964202333
。
我想知道的是如何修改代碼以將該字符串轉換爲圖像並將其插入到PostgreSQL數據庫中,該數據庫具有UTF8
編碼和bytea
數據列類型以存儲圖像。
PHP腳本
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')");
我知道我接近我預期的解決方案。我想我插入解碼的base64字符串,並插入,但錯過了最後一步將其轉換爲Android應用程序採取的原始圖像。對於我的android dev'ing的新穎道歉。
'\ x5265736f75726365206964202333'爲'Resource id#3' as text。所以我會說你沒有獲得自己的圖像數據,但有一些在你的網絡應用程序中處理它。 – 2013-05-13 04:59:17
@CraigRinger我該如何利用這個句柄來查找圖像數據本身? – WebDevDanno 2013-05-13 07:00:11