php - 使用PHP在网络服务中上传多张图片

标签 php android web-services file-upload

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

$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 张图片。任何人都可以帮我吗?提前致谢。

最佳答案

您必须传递文件数组。正如您在评论中提到的,您正在以 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 开发人员的代码。

安卓

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 - 使用PHP在网络服务中上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27834733/

相关文章:

php - 如何在 php 中解密散列密码?使用 password_hash() 方法散列的密码

android - grails spring安全认证 token 问题

java - 将裁剪后的照片保存到 FirebaseStorage 时应用崩溃

javascript - 如何在chrome或android浏览器等移动网络上隐藏地址栏?

java - Web 服务中的多线程

php - GDATA Feed API-用PHP解析时间戳

javascript - 如何将 PHP 数组传递给 JavaScript 函数?

php - MySQL 查询 w/2 个表以获得所需的结果

javascript - 为什么声明 xmlhttp.onreadystatechange 导致这个程序只工作一次?

java - JAXWS 客户端是否区分空集合和空集合值作为返回值?