php - 从android应用程序上传多个图像文件到php服务器

标签 php android cakephp file-upload

我是 android 编程的新手。我一直在尝试使用以下代码将多张图片从我的应用程序上传到服务器。这里 image[] 数组与名称值对中的其他数据(如用户名、密码等)一起发布到服务器。

Java代码:

public void post(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
        for (int index = 0; index < nameValuePairs.size(); index++) {
            if (nameValuePairs.get(index).getName()
                    .equalsIgnoreCase("image[]")) {
                // If the key equals to "image[]", we use FileBody to transfer
                // the data
                Log.d("key", "image");
                Log.d("image path", nameValuePairs.get(index).getName());
                entity.addPart(nameValuePairs.get(index).getName(),
                        new FileBody(new File(nameValuePairs.get(index)
                                .getValue())));

            } else {
                // Normal string data
                Log.d("tag", nameValuePairs.get(index).getName());
                // Log.d("tag", nameValuePairs.get(index).getValue());
                entity.addPart(
                        nameValuePairs.get(index).getName(),
                        new StringBody(nameValuePairs.get(index).getValue()));
            }
        }
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity enttiy = response.getEntity();
        Log.d("server response to post data", EntityUtils.toString(enttiy));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

PHP代码(工作版):

<?php
$target_path = "user_uploaded_photos/";
for($i=0;$i<count($_FILES["image"]["name"]);$i++){
$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));
$username = $_POST['username'];
print_r($fileData);
$date = date('Y_m_d_H_i_s');
$newfilename = $username.$i.$date.".".$fileData['extension'];
if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path."/".$newfilename))
{
    echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery<br />";
}
else
{
 echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!<br />";
}
} $location = $_POST['location'];
//other fields and database operations ommitted

我的问题是我们正在使用的 cakephp 网站。对于 cake php,我使用如下名称值对:

nameValuePair
         .add(new BasicNameValuePair("UserDatum[user_id]", "2"));

像这样发送字符串值对我有用。但是当我声明 images[] 数组图像时,图像被上传但不能从 $_FILES 访问。我已声明 images[] 数组如下:

for (String s : imagePath) {
            nameValuePair.add(new BasicNameValuePair("UserDatum[images][]",
                    s));
            Log.d("image-path", s);
        }

这里的 imagePath 是包含图像路径的字符串数组列表。在应用程序中声明“UserDatum[images][]”是正确的方法吗?它适用于发布的 php,但在 cake php 中,仅发布字符串值而不是图像。我尝试过其他图书馆,例如 ion .但是我只能在不使用数组结构的情况下上传一张照片。请发表评论或为我指明在单个帖子中发布多个图像和字符串的正确方向。

最佳答案

每当我从应用程序发布图像数据时,我都会使用 Base64 编码图像(请参阅:Android post Base64 String to PHP)

这可能会给你一条更好的路线。

关于php - 从android应用程序上传多个图像文件到php服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22502290/

相关文章:

android - List<Class> 上的空指针异常

apache - CakePHP、CGI 和 mod_rewrite

php - 使用 CakePHP 将客户端 ID 更改为客户端名称

javascript - 添加视频链接并在我的网页中播放视频

java - 如何用 2 个项目过滤我的回收 View ?

android - 一些(但不是全部)用户从 Google Play 下载我的应用时收到 "Package file was not signed correctly"

php - 从数据库中获取数据,没有得到正确的数据

php - 在 PHP 中有条件地使用特征

php - 如何在 mysql json 查询中选择多个字段?

php - PHP-警告:为foreach()提供了无效的参数