【问题标题】:Uploading multiple images in webservice using PHP使用 PHP 在 web 服务中上传多个图像
【发布时间】:2015-01-08 07:09:53
【问题描述】:

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

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

【问题讨论】:

  • 你想同时上传4个还是5个文件?解释一下
  • 是的,我需要同时上传 n 个文件
  • 如果从 db 中获取 $data,则在 while 循环中编写上传代码
  • @user3386779 有没有网站可以检查从浏览器上传的文件?因为我需要检查我是如何从用户那里接收数据的

标签: php android web-services file-upload


【解决方案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 开发人员的代码。

安卓

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.

【讨论】:

    【解决方案2】:

    这将帮助您检查您的网络服务

    <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次。

    【讨论】:

    • 感谢您的回复,但我得到的文件是 base64 加密方法,我尝试使用 foreach 循环,但没有得到文件
    • 不是争论!仅供参考,正如您提到的 file1 参数为 multiple。这将是一个数组,所以 move_uploaded_file() 不像你演示的那样工作。
    猜你喜欢
    • 2012-02-14
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2013-04-07
    • 1970-01-01
    • 2012-12-06
    • 2011-12-31
    相关资源
    最近更新 更多