title: Android - File upload works on localhost using Xampp but does not work on server
body: <p>I have made an app that is supposed to let me upload to a server but it only works on localhost. The online server I am using is ueuo.com (free). What might be a solution to this? Funny thing though, the server response code I get is 200.</p>
  
  <p>Here is the app:</p>
  
  <pre><code>import java.io.DataOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import android.net.Uri;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.app.Activity;
  import android.content.Intent;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends Activity {
  
      private static final String JPEG_FILE_PREFIX = "img";
      private static final String JPEG_FILE_SUFFIX = ".jpg";
      private static final String FOLDER_NAME = "/idms/";
      private String imageFileName = "";
  
      Button btnUpload;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          makeDirectory();
  
          btnUpload = (Button) findViewById(R.id.button1);
          btnUpload.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
                          .format(new Date());
                  imageFileName = JPEG_FILE_PREFIX + timeStamp + JPEG_FILE_SUFFIX;
                  File file = new File(Environment.getExternalStorageDirectory()
                          .getPath() + FOLDER_NAME + imageFileName);
                  Uri imageUri = Uri.fromFile(file);
  
                  Intent intent = new Intent(
                          android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                  startActivityForResult(intent, 0);
              }
          });
      }
  
      public void makeDirectory() {
          File dir = new File(Environment.getExternalStorageDirectory()
                  + FOLDER_NAME);
          if (!dir.exists()) {
              dir.mkdir();
          }
      }
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          //String pathToOurFile = Environment.getExternalStorageDirectory()
                  .getPath() + FOLDER_NAME + imageFileName;
          // Upload the image to the server
          new Thread(new Runnable() {
              public void run() {
                   uploadFile();
  
              }
            }).start();        
  
      }
  
      protected void uploadFile() {
          HttpURLConnection connection = null;
          DataOutputStream outputStream = null;
          // DataInputStream inputStream = null;
          String pathToOurFile = Environment.getExternalStorageDirectory()
                  .getPath() + FOLDER_NAME + imageFileName;
          String urlServer = "http://idmstest.ueuo.com/upload_api/uploadtoserver.php";
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024;
          Log.d("tester", pathToOurFile);
          try {
              FileInputStream fileInputStream = new FileInputStream(new File(
                      pathToOurFile));
              URL url = new URL(urlServer);
              connection = (HttpURLConnection) url.openConnection();
              // Allow Inputs &amp; Outputs
              connection.setDoInput(true);
              connection.setDoOutput(true);
              connection.setUseCaches(false);
              // Enable POST method
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Connection", "Keep-Alive");
              connection.setRequestProperty("Content-Type",
                      "multipart/form-data;boundary=" + boundary);
              outputStream = new DataOutputStream(connection.getOutputStream());
              outputStream.writeBytes(twoHyphens + boundary + lineEnd);
              outputStream
                      .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                              + pathToOurFile + "\"" + lineEnd);
              outputStream.writeBytes(lineEnd);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
              // Read file
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              while (bytesRead &gt; 0) {
                  outputStream.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              }
              outputStream.writeBytes(lineEnd);
              outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                      + lineEnd);
              // Responses from the server (code and message)
              @SuppressWarnings("unused")
              int serverResponseCode = connection.getResponseCode();
              @SuppressWarnings("unused")
              String serverResponseMessage = connection.getResponseMessage();
              Log.i("uploadFile", "HTTP Response is : "
                      + serverResponseMessage + ": " + serverResponseCode);
              fileInputStream.close();
              outputStream.flush();
              outputStream.close();
              Log.d("tester", pathToOurFile);
          } catch (Exception ex) {
              // Exception handling
              ex.printStackTrace();
  
  
              Log.e("Upload file to server Exception", "Exception : "
                                               + ex.getMessage(), ex);  
          }
      }
  </code></pre>
  
  <p>Here is the server-side php:</p>
  
  <pre><code>&lt;?php
  
  
      $file_path = "uploads/";
  
      $file_path = $file_path . basename( $_FILES['uploadedfile']['name']);
      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $file_path)) {
          echo "success";
      } else{
          echo "fail";
      }
   ?&gt;
  </code></pre>