3

I want to upload few files from my android code so that it is available in the $_FILES array.

The server is looking for the files as an array. The php code in the server looks like

for ($i=0;$i<$NumFiles;$i++){
   ...
   $filename = $_FILES["fileset"]["name"][$i];
   $filetype = $_FILES["fileset"]["type"][$i];
   $file = curl_image_create($filePath,$filetype,$filename);
   ...
}

I have the files loaded in a File[] (fileSet) for my android code

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost request = new HttpPost(this.apiURL);
        MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();
        StringBody numFilesBody = new StringBody("" + fileSet.length, ContentType.TEXT_PLAIN);
        mpEntity.addPart("NumFiles", numFilesBody);
        for (int i = 0; i < fileSet.length; i++) {
            //What should I be doing here to get the $_FILES set up correctly
        }
        request.setEntity(mpEntity.build());
        HttpResponse response = client.execute(request);

I need to set up the HTT PRequest so that the server can read the files in the $_Files as an array.

6
  • 1
    what is the question? Commented Jul 22, 2015 at 2:53
  • I need to set up the HTTPRequest so that the server can read the files in the $_Files as an array Commented Jul 22, 2015 at 3:41
  • Is your fileSet already an array of ByteArrayBody? Or, what is the datatype of your fileSet object. Commented Jul 22, 2015 at 3:48
  • fileSet is an array of File. Commented Jul 22, 2015 at 4:51
  • for ($i=0;$i<$NumFiles;$i++). How did the server determine `$NumFiles? Please show exact php code. Commented Jul 22, 2015 at 7:52

1 Answer 1

5
File files [] = .....

Then add them in the for loop as:

mpEntity.addPart( "fileset[" + i + "]", new FileBody(files[i]) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for your guidance, any referral links, kindly share here.

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.