Upload multiple images to webservice using PHP

I am new to webservice using PHP for an Android device. I need to work on the concept of uploading multiple images. Please suggest. I implemented a single upload concept, below is the code for downloading a single file.

$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 a time. Can anyone help me with this? Thanks in advance.

+1
source share
2 answers

You need to transfer an array of files. As you noted in the comment, you are sending the file data in base64 format, try the 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);
    }
  }

Android [] POST. prop_images[] .

Android, Android.

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.
+1

-

<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"];
  ?>

n .

+1

Source: https://habr.com/ru/post/1662270/


All Articles