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?

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
AYMADA
  • 889
  • 3
  • 17
  • 37

2 Answers2

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.

keaton_fu
  • 444
  • 2
  • 9
  • I change it but it dont help. – AYMADA Jul 13 '12 at 06:56
  • 1
    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. – keaton_fu Jul 13 '12 at 13:40
  • 1
    Also, I found a few SO threads with that same PHP snippet, so I don't know where you found it, but [this](http://stackoverflow.com/questions/4295417/upload-a-picture-from-android-to-php-server) thread suggests looking at your server's settings and [this](http://stackoverflow.com/questions/1314249/upload-and-post-file-to-php-page) one has some accompanied Java code that you could try. – keaton_fu Jul 13 '12 at 13:44
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
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81