Pergunta

I'm trying to upload an image from Android to a PHP server.

Here is my upload code:

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();
    }
}

Here is my PHP code for handling the upload:

<?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";
    }
?>

I'm getting 200 responses from the page, but the $_FILES and $_REQUEST variables are both empty. It seems that the image file is not making it to the script, and I have no idea why. I'm doing it right according to all the tutorials I've found.

The images I'm uploading are ~180kb

Any ideas?

Foi útil?

Solução

This was a problem with my server. I switched to using a different sub-domain of my server, and it's working perfectly now.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top