首页 > 解决方案 > 使用php将图像从android上传到服务器

问题描述

我正在尝试存储从 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 发送请求并且弹出了这些错误。

提前致谢!

标签: phpandroidimageserver

解决方案


推荐阅读