2

We have API which receives images converted in base64 string. Our mobile application consumes too much RAM during the conversion process (to base64), now we need to upload image as multipart. I developed the mobile part but I'm stuck with the PHP API. We switched from volley to Retrofit because volley did not support multipart upload.

What do I need to change in the script that receives the multipart image upload?

<?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"
    ));
}
?>
jasonlam604
  • 1,456
  • 2
  • 16
  • 25
Adnan
  • 7,825
  • 3
  • 23
  • 41
  • i have custom methods [here](http://stackoverflow.com/questions/36513174/android-multipart-image-upload-with-httpconnectionparams-deprecated-in-new-api/36513504#36513504) for upload pics it work by Json maybe help you. – mehrdad khosravi May 27 '16 at 07:29
  • Please remove the database code from your script. It has nothing to do with receiving an image. Concentrate on receiving. Keep the script small. – greenapps May 27 '16 at 07:41

2 Answers2

0

Have a look at move_uploaded_file() function of php and the $_FILES array.

Example code plenty on this site.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

If you want add multipart upload in backend you should make next changes:

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

Also, make shore you use post request with multipart format (it should have header Content-Type: multipart/form-data and bi in right format - https://ru.wikipedia.org/wiki/Multipart/form-data). Hope it help you.

Lakremon
  • 787
  • 1
  • 8
  • 26