使用PHP在Web服务中上传多个图像 [英] Uploading multiple images in webservice using PHP

查看:104
本文介绍了使用PHP在Web服务中上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不熟悉使用Android设备上的PHP进行网络服务的.我需要研究多个图像上传的概念.请提出建议.我已经实现了单上传的概念,单文件上传的代码如下.

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

我需要同时上传n张图片的代码.有人可以帮我吗?预先感谢.

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

推荐答案

您必须传递文件数组.正如您在评论中提到的那样,您正在以base64格式发送文件数据,请尝试使用PHP的以下代码.

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

在android代码中,请确保在发出POST请求时在参数名称中添加[] . 按照我上面给出的示例,该参数应为prop_images[].

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.

我不是Android开发人员,但是我可以从我们的Android开发人员发布代码.

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.

这篇关于使用PHP在Web服务中上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆