0

I'm writing an app which should send a file to PHP server. Here is my code:

    InputStream is = new FileInputStream(file);
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("http://majkelsoftgames.cba.pl/ser/server.php");

    byte[] data = IOUtils.toByteArray(is);
    InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");

    MultipartEntity multipartContent = new MultipartEntity();
    multipartContent.addPart("file", isb);

    postRequest.setEntity(multipartContent);
    HttpResponse response = httpClient.execute(postRequest);

My problem is that I really dont have experience in PHP and I dont know how to pick up this file on PHP side. I found some code:

<?php
    $target_path  = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
    } 
    else {
        echo "There was an error uploading the file, please try again!";
    }
?>

But it does not work. Can someone explain me how can I simply pick up fine on PHP side?

2 Answers 2

2

I think the issue is here:

InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");

This is sending the data with the name "file", but then

$_FILES['uploadedfile']['name']

is trying to find a file named "uploadedfile". Make sure they match.

Sign up to request clarification or add additional context in comments.

3 Comments

I change it but it dont help.
Okay, well then I need some more details. What do you mean when you said "it does not work"? Are you getting a response with one of those echo statements or no response at all? This way you know if you are even hitting the page correctly.
Also, I found a few SO threads with that same PHP snippet, so I don't know where you found it, but this thread suggests looking at your server's settings and this one has some accompanied Java code that you could try.
1
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event...

if (isset($_POST['submit']))
{

if ( ($_FILES["file"]["type"] =="application/pdf"))
 { 

 if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]))

    echo " This File is already exists in folder";

else
{
  move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);      
  echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ "  . $_FILES["file"]["name"];

  }
}

}//end of click_event

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.