how to upload more than one image to server(mysql database) using php and android

前端 未结 2 2011
无人共我
无人共我 2021-01-15 02:39

I am referring this code in my project..code snippet. Here i can able to upload one image succesfully..Now i have to upload more than one image..How can i do that one..I di

相关标签:
2条回答
  • 2021-01-15 03:01

    Please refer to my answer to the question:

    Save multiple image into mysql php from android but only one image get inserted

    Edit:

    In your PHP sript you are overwriting the image upload because you are using the same upload path for both images.

    You must make sure the $path value is unique .

    Try this script:

    <?php
    
    if($_SERVER['REQUEST_METHOD']=='POST'){
    
        define('HOST','hostname');
        define('USER','username');
        define('PASS','password');
        define('DB','dbname');
    
        $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
    
        $path = "uploads/".uniqid().".png";
        $path1 = "uploads/".uniqid().".png";
    
        $actualpath = "http://oursite/PhotoUploadWithText/$path";
        $actualpath1 = "http://oursite/PhotoUploadWithText/$path1";
    
        $sql = "INSERT INTO uploads (image,image1,name) VALUES ('$actualpath','$actualpath1','$name')";
    
        if(mysqli_query($con,$sql)){
            file_put_contents($path,base64_decode($image));
            file_put_contents($path1,base64_decode($image1));
            echo "Successfully Uploaded";
        }
    
        mysqli_close($con);
    
    }else{
        echo "Error";
    }
    
    0 讨论(0)
  • 2021-01-15 03:01

    You can use $id = uniqid(); in order to get different id for the images.

    In your java code, change private int PICK_IMAGE_REQUEST1 = 1; to private int PICK_IMAGE_REQUEST1 = 2;

     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case PICK_IMAGE_REQUEST:
                    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK & null != data) {
                       Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
                    }
    
                    break;
    
                case PICK_IMAGE_REQUEST1:
                    if (requestCode == PICK_IMAGE_REQUEST1 && resultCode == RESULT_OK) {
                        Uri filePath = data.getData();
        try {
            //bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //imageView.setImageBitmap(bitmap);
            imageView1.setImageBitmap(bitmap1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        }
    
    0 讨论(0)
提交回复
热议问题