如何在PHP中接收Multipart请求

时间:2016-05-27 07:25:47

标签: php android api multipart

我们有API接收以base64字符串转换的图像。我们的移动应用程序在转换过程中消耗了太多RAM(到base64),现在我们需要将图像上传为多部分。我开发了移动部件,但我坚持使用PHP API。我们从凌空转换到Retrofit,因为凌空不支持分段上传。

在接收多部分图片上传的脚本中需要更改哪些内容?

<?php

//header('Content-Type : bitmap; charset=utf-8');
//header('Content-Type: application/json');

if (isset($_POST["encoded_string"])) {

    //encoded_string -> base64 string sent from mobile phone

    $encoded_string = $_POST["encoded_string"];
    $image_name     = $_POST["image_name"];

    $decoded_string = base64_decode($encoded_string);

    $path = 'images/' . $image_name;

    if (!file_exists('images')) {
        mkdir('images', 0777, true);
    }

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

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

    if ($is_written > 0) {

        $connection = mysqli_connect('localhost', 'root', '', 'test');

        if ($connection) {

            $query = "INSERT INTO photos(name,path) values('$image_name','$path');";

            $result = mysqli_query($connection, $query);

            if ($result) {
                echo json_encode(array(
                   "response" => "Success! Image is succefully uploaded!.",     
                   "result" => "success"
                ));
            } else {
                echo json_encode(array(
                    "response" => "Error! Image is not uploaded.",
                    "result" => "error"
                ));
            }
            mysqli_close($connection);
        } else {
            echo json_encode(array(
                "response" => "Error! No database connection!",
                "result" => "error"
            ));
        }
    }
} else {
    echo json_encode(array(
        "response" => "Error! Please insert data!",
        "result" => "error"
    ));
}
?>

2 个答案:

答案 0 :(得分:0)

查看php的move_uploaded_file()函数和$_FILES数组。

此网站上的示例代码很多。

答案 1 :(得分:0)

如果您想在后端添加分段上传,则应进行下一次更改:

<?php

//header('Content-Type : bitmap; charset=utf-8');
//header('Content-Type: application/json');


if (isset($_POST["encoded_string"])) {


    //encoded_string -> base64 string sent from mobile phone
    if (!file_exists('images')) {
        mkdir('images', 0777, true);
    }

    $connection = mysqli_connect('localhost', 'root', '', 'test');

    if (!$connection) {
        echo json_encode(array(
            "response" => "Error! No database connection!",
            "result" => "error"
        ));
        die;
    }

    $responses = array();

    foreach ($_FILES["pictures"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
            $image_name = $_FILES["pictures"]["name"][$key];
            $path = 'images/' . $image_name;
            if (move_uploaded_file($tmp_name, path)) {
                $query = "INSERT INTO photos(name,path) values('$image_name','$path');";
                $result = mysqli_query($connection, $query);
                if ($result) {
                    $responses[] = array(
                        "response" => "Success! Image is succefully uploaded!.",
                        "result" => "success"
                    );
                } else {
                    $responses[] = array(
                        "response" => "Error! Image is not uploaded.",
                        "result" => "error"
                    );
                }
            }
        } else {
            $responses[] = array(
                "response" => "Error! Please insert data!",
                "result" => "error"
            );
        }
    }
    mysqli_close($connection);
    echo json_encode(array(
        'responses' => $responses
    ));
}

另外,请使用多部分格式的帖子请求(它应该包含标题Content-Type:multipart / form-data和bi格式正确 - https://ru.wikipedia.org/wiki/Multipart/form-data)。希望对你有所帮助。