使用php将图像从android上传到服务器

时间:2018-05-06 18:33:02

标签: php android image server

我正在尝试存储从Android Studio(来自用户的图库)拍摄的图像,并使用php将base64编码的图像转换为字符串以存储在网络服务器上。

我一直在寻找以不同方式暗示这一点的众多教程。我选择使用fopen()函数完成一个,但它一直给我一个“找不到文件或目录”错误。

之后我尝试编写图像目录的完整路径,我将保存图像。错误更改为我在代码的“else”部分中定义的错误消息,即:

  

{“error”:true,“error_msg”:“创建优惠时出现未知错误。请重试。”}

这是完整的代码:

class create_offer_form {

private $conn;

// constructor
function __construct() {
    require_once 'android_login_connect.php';
    // connecting to database
    $db = new android_login_connect();
    $this->conn = $db->connect();
}

public function StoreOfferInfo($offerMname, $offerUname, $offerTitle, $offerText, $offerBranch, $offerDateTime, $offerMinPrice, $offerMaxPrice, $offerCategory, $offerImageURL) {


        $image_name = time()."_".rand().".jpeg" ;


        $decoded_string = base64_decode($offerImageURL);

        $path = 'AndroidUploadedImages/'.$image_name;

        $file = fopen($path, 'wb');

        $is_written = fwrite($file, $decoded_string);
        fclose($file);

        if($is_written > 0) {

            $stmt = $this->conn->prepare("INSERT INTO Image(Image) VALUES(?)");
            $stmt->bind_param("s", $path);
            $result = $stmt->execute();
            $stmt->close();

            if($result){
                echo "success";
            }else{
                echo "failed";
            }

        }


    $stmt = $this->conn->prepare("INSERT INTO Offer(Offer_title, Offer_memberName, Offer_branch, Offer_text, Offer_image, Offer_DateTime, Offer_minPrice, Offer_maxPrice, Offer_category, Offer_Username) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssssssss", $offerTitle, $offerMname, $offerBranch, $offerText, $offerImageURL, $offerDateTime, $offerMinPrice, $offerMaxPrice, $offerCategory, $offerUname);
    $result = $stmt->execute();
    $stmt->close();

    // check for successful store
    if ($result) {
        $stmt = $this->conn->prepare("SELECT Offer_title, Offer_memberName, Offer_branch, Offer_text, Offer_image, Offer_DateTime, Offer_minPrice, Offer_maxPrice, Offer_category, Offer_Username FROM Offer WHERE Offer_title = ?");
        $stmt->bind_param("s", $offerTitle);
        $stmt->execute();
        $stmt-> bind_result($token1, $token2,$token3,$token4,$token5,$token6, $token7, $token8, $token9, $token10); 

        while ( $stmt-> fetch() ) {
           $offer["offerMname"] = $offerMname;
           $offer["offerUname"] = $offerUname;
           $offer["offerTitle"] = $offerTitle;
           $offer["offerText"] = $offerText;
           $offer["offerBranch"] = $offerBranch;
           $offer["offerDateTime"] = $offerDateTime;
           $offer["offerMinPrice"] = $offerMinPrice;
           $offer["offerMaxPrice"] = $offerMaxPrice;
           $offer["offerCategory"] = $offerCategory;
           $offer["offerImageURL"] = $offerImageURL;
        }
        $stmt->close();
        return $offer;
    } else {
      return false;
    }
}

这是第一个错误报告错误的LOC:

        $file = fopen($path, 'wb');

        $is_written = fwrite($file, $decoded_string);
        fclose($file);

Android Studio中的图像在ImageView中显示得很好,并且还使用base64成功编码。我知道错误来自php的方式是因为我试图使用Postman发送请求并弹出这些错误。

提前致谢!

0 个答案:

没有答案