【问题标题】:Android image upload not getting to PHP scriptAndroid图像上传没有进入PHP脚本
【发布时间】:2014-02-12 14:57:10
【问题描述】:

我正在尝试将图像从 Android 上传到 PHP 服务器。

这是我的上传代码:

public static void uploadFile(final String imagePath){

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(SERVER);

    try {
        File imageFile = new File(imagePath);

        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));

        MultipartEntityBuilder entity = MultipartEntityBuilder.create();

        entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("image", new FileBody(imageFile));

        httpPost.setEntity(entity.build());
        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity resEntity = response.getEntity();

        BufferedReader reader = new BufferedReader(new InputStreamReader(resEntity.getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我处理上传的 PHP 代码:

<?php
    echo "FILES - ";
    var_dump($_FILES);
    echo " REQUEST - ";
    var_dump($_REQUEST);

    $file_path = "images";

    $file_path = $file_path . basename($_FILES['image']['name']);

    if(move_uploaded_file($_FILES['image']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
?>

我从页面收到 200 条响应,但 $_FILES 和 $_REQUEST 变量都是空的。似乎图像文件没有进入脚本,我不知道为什么。根据我找到的所有教程,我做得对。

我上传的图片约为 180kb

有什么想法吗?

【问题讨论】:

    标签: php android image-uploading


    【解决方案1】:

    这是我的服务器的问题。我切换到使用服务器的不同子域,现在它运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2013-07-22
      • 2012-05-15
      • 2012-01-31
      • 1970-01-01
      • 2011-12-11
      • 2011-07-30
      相关资源
      最近更新 更多