phpandroidandroid-asynctaskmultipartentity

how do i upload multiple images using Multiparty Entity in Android


i have to upload multiple images from my android device but i was stuck to thinks how do i send array of images.

so i have finally decided put my PHP code:

  $id=$_REQUEST['id'];

$tot_img=count($_FILES['uploads']['name']);
$of_img='';
for($i=0;$i<$tot_img;$i++)
{
    if($_FILES['uploads']['name'][$i]!="")
    {
        $path=$_SERVER['DOCUMENT_ROOT']."/uploads/offer_img/";
        $logo_name=$_FILES['uploads']['name'][$i];
        list($name,$ext)=explode('.',$_FILES['uploads']['name'][$i]);
        $logo_name=$name.$i.date('His').'.'.$ext;
        $path=$path.$logo_name;
        move_uploaded_file($_FILES['uploads']['tmp_name'][$i],$path);
        if($of_img!='')
        {
            $of_img=$of_img.','.$logo_name;
        }
        else
        {
            $of_img=$logo_name;
        }

    }
}

so please tell me how i can send images.

in Advance Thank you.


Solution

  • I use the following code in android to upload the multiples images from android

                private void doFileUpload(List<Uri> mDetials){
                        List<File> mFiles=new ArrayList<>();
                        for(int i=0;i<mDetials.size();i++){
                            String mFileLocation=getPath(getApplicationContext(),mDetials.get(i));
                            File file = new File(mFileLocation);
                            mFiles.add(file);
                        }
                        String urlString = "php_file_link.php";
                        try {
                            HttpClient client = new DefaultHttpClient();
                            HttpPost post = new HttpPost(urlString);
                            List<FileBody> mFileBody=new ArrayList<>();
                            for(int i=0;i<mFiles.size();i++){
                                FileBody bin1 = new FileBody(mFiles.get(i));
                                mFileBody.add(bin1);
                            }
                            MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
                            reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                            for(int i=0;i<  mFileBody.size();i++){
                                reqEntity.addPart("uploadedfile"+i,mFileBody.get(i));
                            }
                            reqEntity.addTextBody("size",""+mDetials.size(), ContentType.TEXT_PLAIN);
                            post.setEntity(reqEntity.build());
                            HttpResponse response = client.execute(post);
                            HttpEntity resEntity = response.getEntity();
                            final String response_str = EntityUtils.toString(resEntity);
    
                            if (resEntity != null) {
                                Log.i("RESPONSE", response_str);
                                dialog.dismiss();
                                runOnUiThread(new Runnable() {
                                    public void run() {
                                        try {
                                            Toast.makeText(getApplicationContext(), response_str, Toast.LENGTH_LONG).show();
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }});
                            }
                        } catch (Exception ex){
                            Log.e("Debug", "error: " + ex.getMessage(), ex);
                            dialog.dismiss();
                        }
                }
    

    I use the following php code to upload on server

    $size = $_REQUEST['size'];
    for ($x = 0; $x < $size; $x++) {
    $target_path="../GizboImages/customer_care/";
    $target_path=$target_path.basename( $_FILES["uploadedfile".$x]['name']);
    if(move_uploaded_file($_FILES["uploadedfile".$x]['tmp_name'], $target_path)) ;
    }