1

I am new to webservice using PHP for Android device. I need to work on multiple image upload concept. Please suggest. I have implemented single upload concept the code for single file upload is given below.

$data = $_REQUEST;
if($data["prop_images"]){       
            $filename = md5(time()).'.jpg';
            $base=$data["prop_images"];
            $binary = base64_decode($base);         
            $pathtoupload = JPATH_ADMINISTRATOR . '/components/com_clinchproperties/galupload/';
            //header('Content-Type: bitmap; charset=utf-8');  // binary, utf-8 bytes
            $actual_image_name = time().".jpg";
            $image = $filename;
            $file = fopen($pathtoupload.$filename,  'wb');
            fwrite($file, $binary);
            fclose($file);
        }

I need code to upload n number of images at same time. Can any one help me with it? Thanks in advance.

4
  • do you want to upload 4 or 5 files in same time?explain Commented Jan 8, 2015 at 7:15
  • yes I need n number of files to be uploaded at same time Commented Jan 8, 2015 at 7:17
  • write upload code in while loop if you fetch $data from db Commented Jan 8, 2015 at 7:20
  • @user3386779 is there any website where we can check the file upload from browser? because I need to check how I receive the data from user Commented Jan 8, 2015 at 7:24

2 Answers 2

1

You have to pass array of files. As you mentioned in comment, you are sending file data in base64 format, try following code for PHP.

PHP

  $data = $_REQUEST;
  if($data["prop_images"]){ 
    foreach($data["prop_images"] as $img){ //array of images. So loop for every images
        $filename = md5(time()).'.jpg';
        $base=$img;
        $binary = base64_decode($base);         
        $pathtoupload = JPATH_ADMINISTRATOR . '/components/com_clinchproperties/galupload/';
        $actual_image_name = time().".jpg";
        $image = $filename;
        $file = fopen($pathtoupload.$filename,  'wb');
        fwrite($file, $binary);
        fclose($file);
    }
  }

In android code, make sure to add [] in parameter name while making POST request. That parameter should be prop_images[] as per example I given above.

I'm not an Android developer, but I can post code from our Android developer.

Android

HttpClient httpClient = new DefaultHttpClient();

HttpPost postRequest = new HttpPost("http://webserver.com/path/to/webservice.php");

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

for (int i = 0; i < number_of_images; i++) {
    //convert your images to base64 and store in base64ImageData.
    reqEntity.addPart("prop_images[]", base64ImageData);   //adding parameter
}

//execute request.
Sign up to request clarification or add additional context in comments.

Comments

1

This will help you to check your web service

<form method="post" enctype="multipart/form-data" action="audioupload.php">
 <input type="file" name="file1" multiple>
 <input type="submit"  value="OK">
</form>

audio.php
 <?php  
   move_uploaded_file($_FILES["file1"]["tmp_name"],"audio/".$_FILES["file1"]["name"]);   
  $url = "audio/".$_FILES["file1"]["name"];
  ?>

likewise developer can call this service n times.

2 Comments

Thanks for your reply but I am getting the file is base64 encryption method I have tried using foreach loop but it is not getting the files
Not an argument! Just for your information, as you mentioned file1 parameter as multiple. It will be an array so, move_uploaded_file() doesn't work as the way you demonstrated.

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.