php,用于从MultipartEntity接收图像和文本 [英] php for receiving image and text from MultipartEntity

查看:118
本文介绍了php,用于从MultipartEntity接收图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过MultipartEntity上传图像和一些文本. 我可以上传和接收图像,但是当我尝试添加Stringbody时,似乎无法接收它. 这是我的android代码

I'm trying to upload an image and some text via MultipartEntity. I can upload and receive the image, but when I try to add a Stringbody I cannot seem to receive it. Here's my android code

imports ETC...

public void oncreate(){
.....
nameValuePairs.add(new BasicNameValuePair("image", exsistingFileName));
nameValuePairs.add(new BasicNameValuePair("title", "title"));
}

public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
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")) {
            System.out.println("post - if");
            // If the key equals to "image", we use FileBody to transfer the data
            entity.addPart( nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));

        } else {
            System.out.println("post - else");
            // Normal string data
            entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
        }
    }
    System.out.println("post - done" + entity);

    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost, localContext);

} catch (IOException e) {
    e.printStackTrace();
}
}

还有我的php:

<?php

$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"]["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

我一直在讨论有关MultipartEntity的其他问题,但似乎找不到答案.我试过只发送Stringbody,但也没有任何成功.我认为问题出在服务器端(在PHP中),但是任何建议都是可以接受的.

I've been around the other questions about MultipartEntity, but cannot seem to find the answer. I've tried sending just the Stringbody, but didn't have any succs in that either. I think the problem is serverside (in the PHP) but any suggestions are welcome.

这是我在这里的第一个问题-请随时评论形式和清晰度:-)

This is my first question in here - feel free to comment on form and clarity :-)

推荐答案

问题出在我的php上. 当您收到Stringbody时,只有一个参数(与filebody相反).因此,我删除了$ uploadtitle = $ _FILES ["title"] ["title"];中的第二个参数.并且有效

The problem was i the php. When you receive Stringbody there is only one parametre(as opposed to filebody). So I removed the second parametre in $uploadtitle = $_FILES["title"]["title"]; and it worked

<?php
$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

如果您遇到同样的问题,希望对您有所帮助.

I hope this helps if you have the same problem.

这篇关于php,用于从MultipartEntity接收图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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