使用PHP在Web服务中上载多个图像

时间:2015-01-08 07:09:53

标签: php android web-services file-upload

我是使用PHP for Android设备的webservice新手。我需要处理多个图像上传概念。请建议。我已经实现了单个上传概念,下面给出了单个文件上传的代码。

$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);
        }

我需要代码同时上传n个图像。任何人都可以帮助我吗?提前谢谢。

2 个答案:

答案 0 :(得分: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 :(得分:1)

您必须传递文件数组。正如您在评论中提到的那样,您要发送base64格式的文件数据,请尝试使用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.