Can not Upload the file from android to php server
<p>I m making an application which could upload a file from sd card to php server but it gives me the error. My android code is as below:</p>

<pre><code>package de.fileupload;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import android.os.Bundle;

@SuppressWarnings("unused")
public class FileUpload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView tmp = (TextView) findViewById(R.id.textView1);
    tmp.setText("Hi! Click the button!");

    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    File f = new File("mnt/sdcard/SMSBackup.txt");
    try {
    f.createNewFile();
    Date d = new Date();
    PrintWriter writer = new PrintWriter(f);
    writer.println(d.toString());
    writer.close();
    HttpClient client = new DefaultHttpClient();
    httpPostFileUpload(client, "mnt/sdcard/SMSBackup.txt", "http://10.0.2.2:8080/admin/admin/upload1.php", "uploadedfile");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    });
    }
    public void httpPostFileUpload(
    HttpClient client,
    String filePath,
    String uploadUri,
    String inputNameAttr) throws ClientProtocolException,
    IOException {
    HttpUriRequest request = new HttpPost(uploadUri);
    MultipartEntity form = new MultipartEntity();

    client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
    form.addPart(inputNameAttr, new FileBody(new File(filePath)));
    ((HttpEntityEnclosingRequestBase) request).setEntity(form);
    try {
    client.execute(request);
    } catch (ClientProtocolException e) {
    throw e;
    } catch (IOException ee) {
    throw ee;
    }
    }
    }
</code></pre>

<p>and my php files are as below:
upload1.php</p>

<pre><code>&lt;meta name="generator" content="Namo WebEditor(Trial)"&gt;
&lt;form enctype="multipart/form-data" action="upload.php" method="POST"&gt; 
&lt;input type="hidden" name="MAX_FILE_SIZE" value="100000" /&gt; 
 Choose a file to upload: &lt;input name="uploadedfile" type="file" /&gt;&lt;br /&gt;&lt;input 
 type="submit" value="Upload File" /&gt; 
 &lt;/form&gt; 
 &lt;?php 
 $to_file = "tmp/" . basename($_FILES['uploadedfile']['name']); 
 $from_file = $_FILES['uploadedfile']['tmp_name']; 
 if (move_uploaded_file($from_file, $to_file)) { 
  echo "Successful upload"; 
 ?&gt; 
 &lt;a href="&lt;?php echo $to_file;?&gt;"&gt;&lt;?php echo $to_file;?&gt;&lt;/a&gt; 
 &lt;?php 
 } else { 
 echo "Unsuccessful upload"; 
 } 
 ?&gt; 
</code></pre>

<p>and upload.php is:</p>

<pre><code>&lt;?php
// Where the file is going to be placed 
$target_path = "localhost/admin/admin/uploads/";

/* Add the original filename to our target path.  
 Result is "uploads/filename.extension" */
 $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";
 chmod ("uploads/".basename( $_FILES['uploadedfile']['name']), 0644);
 } else{
 echo "There was an error uploading the file, please try again!";
 echo "filename: " .  basename( $_FILES['uploadedfile']['name']);
 echo "target_path: " .$target_path;
 }
 ?&gt;
</code></pre>

<p>Can any one please tell that where i m wrong? It always shows the unsuccessful upload for the file. While my server is running. </p>
