Upload multiple image files to php server from android application

I am new to Android programming. I am trying to upload multiple images from my application to the server using the following code. Here, the image [] array is sent to the server with other data, such as username, password, etc., which is located in the name of valuepair.

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 use. For pake php, I use name value pairs as follows:

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

Sending string values ​​how this works for me. But when I declare the images [], the images of the array are loaded, but cannot be accessed from $ _FILES. I declared an array of images [] as follows:

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

Here, imagePath is an array of List lines containing the path to the images. Is the declaration UserDatum [images] [] in the application in the correct way? It works with published php, but in cake php only string values ​​are published, not images. I tried other libraries like ion , but I can upload one photo without using an array structure. Please comment or point me in the right direction to post multiple images and lines in a single post.

+6
source share
1 answer

Whenever I send image data from an application, I use a Base64 encoded image (see Android post Base64 String for PHP )

This may give you a better route.

0
source

All Articles