base64_decode不适用于PHP

时间:2018-02-26 23:25:13

标签: php android mysql image blob

我目前正在尝试存储Android应用程序中的图像。该应用程序将ImageView对象中的图像转换为字节数组,然后使用Android内置的Base64.encodeToString函数,以便我可以在HTTP Post请求中将其传递给我的PHP脚本(它执行Insert to Database logic )。

出于某种原因,如果在我的PHP脚本中我尝试在将图像存储为MEDIUMBLOB之前调用base64_decode,则整个插入过程会失败,但如果我跳过PHP脚本中的base64_decode,则插入成功。谁能解释一下为什么?已经调试了几个小时,但似乎无法找出原因

我在想解码它可以帮助我节省数据库上的存储空间。我知道没有将图像存储在数据库中并使用路径和东西但是为了我目前的目的,我选择将它存储在数据库中,因为它对我来说更方便(它&#39 ; s不是一个巨大的可扩展项目,因为我只是在为一项小型研究开发一些东西)。

提前致谢!

<?php

    /*
    * Following code will create a new product row
    * All product details are read from HTTP Post Request
    */

    // array for JSON response
    $response = array();

    // check for required fields
    if (isset($_POST['username']) && isset($_POST['drinkName']) && isset($_POST['caption']) && isset($_POST['photo']) ) 
    {

    $username = $_POST['username'];
    $drinkName = $_POST['drinkName'];
    $caption = $_POST['caption'];

    $photoRaw = $_POST['photo'];    
    $photo = base64_decode($photoRaw);

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO Memories(username, drinkName, caption, photo) VALUES('$username', '$drinkName', '$caption', '$photo')");
    // check if row inserted or not
    if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Product successfully created.";
    // echoing JSON response
    echo json_encode($response);
    } else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
    }
    } else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
    }
    ?>

0 个答案:

没有答案