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.