6

I am new to android programming. I have been trying to upload multiple images from my app to server using the following code. Here image[] array is posted to server with other data like username, password,etc which is in namevaluepair.

Java code:

public void post(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
        for (int index = 0; index < nameValuePairs.size(); index++) {
            if (nameValuePairs.get(index).getName()
                    .equalsIgnoreCase("image[]")) {
                // If the key equals to "image[]", we use FileBody to transfer
                // the data
                Log.d("key", "image");
                Log.d("image path", nameValuePairs.get(index).getName());
                entity.addPart(nameValuePairs.get(index).getName(),
                        new FileBody(new File(nameValuePairs.get(index)
                                .getValue())));

            } else {
                // Normal string data
                Log.d("tag", nameValuePairs.get(index).getName());
                // Log.d("tag", nameValuePairs.get(index).getValue());
                entity.addPart(
                        nameValuePairs.get(index).getName(),
                        new StringBody(nameValuePairs.get(index).getValue()));
            }
        }
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity enttiy = response.getEntity();
        Log.d("server response to post data", EntityUtils.toString(enttiy));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Php code (Working version):

<?php
$target_path = "user_uploaded_photos/";
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
$username = $_POST['username'];
print_r($fileData);
$date = date('Y_m_d_H_i_s');
$newfilename = $username.$i.$date.".".$fileData['extension'];
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path."/".$newfilename))
{
    echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />";
}
else
{
 echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} $location = $_POST['location'];
//other fields and database operations ommitted

My problem is with the cakephp website we are using. For cake php I use name value pairs as follows:

nameValuePair
         .add(new BasicNameValuePair("UserDatum[user_id]", "2"));

Sending string values like this works for me. But when I declare images[] array images are uploaded but cannot be accessed from $_FILES. I have declared images[] array as follows:

for (String s : imagePath) {
            nameValuePair.add(new BasicNameValuePair("UserDatum[images][]",
                    s));
            Log.d("image-path", s);
        }

Here imagePath is arrayList of string containing path of images. Is declaring "UserDatum[images][]" in app the correct way to do it? It works with the posted php but in cake php only string values are posted and not images. I have tried other librarys like ion . But I am only able to upload one photo without using array structure. Please comment or point me to the right direction for posting multiple images and strings in a single post.

3
  • Is there nothing at all in $_FILES when you submit the data from your android app? Commented Mar 19, 2014 at 16:07
  • in cake php server there is noting in $_FILES and in the posted php code it works like a charm everything is ok Commented Mar 20, 2014 at 3:43
  • Note: echo $_FILES['image']['name'][$i] might not be safe. Commented Jul 18, 2015 at 11:09

1 Answer 1

0

Whenever I post image data from an app, I use Base64 encoded image (see: Android post Base64 String to PHP)

That might give you a better route.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.