Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中接收多部分请求_Php_Android_Api_Multipart - Fatal编程技术网

如何在PHP中接收多部分请求

如何在PHP中接收多部分请求,php,android,api,multipart,Php,Android,Api,Multipart,我们有API,它接收转换成base64字符串的图像。我们的移动应用程序在转换过程(到base64)中消耗了太多的RAM,现在我们需要将图像作为多部分上传。我开发了移动部分,但我仍然坚持使用PHPAPI。我们从截击切换到改装,因为截击不支持多部分上传 我需要在接收多部分图像上载的脚本中更改什么 <?php //header('Content-Type : bitmap; charset=utf-8'); //header('Content-Type: application/json');

我们有API,它接收转换成base64字符串的图像。我们的移动应用程序在转换过程(到base64)中消耗了太多的RAM,现在我们需要将图像作为多部分上传。我开发了移动部分,但我仍然坚持使用PHPAPI。我们从截击切换到改装,因为截击不支持多部分上传

我需要在接收多部分图像上载的脚本中更改什么

<?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"
    ));
}
?>

看看php的
move\u upload\u file()
函数和
$\u FILES
数组


此站点上有大量示例代码。

如果您想在后端添加多部分上载,则应进行下一步更改:

<?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
    ));
}

我有上传图片的自定义方法,它通过Json工作可能会对您有所帮助。请从脚本中删除数据库代码。它与接收图像无关。专注于接收。保持脚本小。