title: Upload file to server Android
body: <p>I want to upload some mp3 files to my localhost server however I am not sure where I am going wrong. I think it is the <code>php</code> file, but here is my <code>php</code> file:</p>
  
  <pre><code>&lt;?php
      $file_path = "/storage/emulated/0/sample.txt";
      $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
      if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
          echo "success";
      } else{
          echo "fail";
      }
   ?&gt;
  </code></pre>
  
  <p>This is my <code>java</code> code:</p>
  
  <pre><code>public class UploadFile extends Activity {
      int serverResponseCode = 0;
      String serverResponseMessage;
      String uploadFileName;
      String uploadFilePath;
      String upLoadServerUri = null;
      ProgressBar loading;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.uploads);
          uploadFilePath = "/storage/emulated/0/";
          uploadFileName = "sample.txt";
  
          upLoadServerUri = "http://10.0.3.2/files/uploadfile.php";
          new Thread(new Runnable() {
  
              public void run() {
                  uploadFile(uploadFilePath + "" + uploadFileName);
              }
          }).start();
      }
  
      public void uploadFile(String sourceFileUri) {
          String fileName = sourceFileUri;
          HttpURLConnection conn = null;
          DataOutputStream dos = null;
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int code;
  
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024;
  
          File sourceFile = new File(sourceFileUri);
  
          if (!sourceFile.isFile()) {
              code = 0;
              this.runOnUiThread(new Runnable() {
                  public void run() {
  
                  }
              });
          }
          else {
              try {
                  FileInputStream fileInputStream = new FileInputStream(sourceFile);
                  URL url = new URL(upLoadServerUri);
  
                  conn = (HttpURLConnection) url.openConnection();
                  conn.setDoInput(true); // Allow Inputs
                  conn.setDoOutput(true); // Allow Outputs
  
                  conn.setUseCaches(false); // Don't use a Cached Copy
                  conn.setRequestMethod("POST");
                  conn.setRequestProperty("Connection", "Keep-Alive");
                  conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                  conn.setRequestProperty("uploaded_file", fileName);
                  dos = new DataOutputStream(conn.getOutputStream());
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name='uploaded_file';filename='"+fileName+"'"+lineEnd);
                  dos.writeBytes(lineEnd);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  while (bytesRead &gt; 0) {
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  }
  
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                  serverResponseCode = conn.getResponseCode();
                  code = serverResponseCode;
                  if (code==200) {
                      this.runOnUiThread(new Runnable() {
                          public void run() {
  
                          }
                      });
                  }
                  else {
                      this.runOnUiThread(new Runnable() {
                          public void run() {
  
                          }
                      });
                  }
                  serverResponseMessage = conn.getResponseMessage();
                  fileInputStream.close();
                  dos.flush();
                  dos.close();
              }
              catch (MalformedURLException ex) {
                  this.runOnUiThread(new Runnable() {
                      public void run() {
  
                      }
                  });
              }
              catch (Exception e) {
                  this.runOnUiThread(new Runnable() {
                      public void run() {
  
                      }
                  });
              }
          }
      }
  }
  </code></pre>
  
  <p>I got this code online and modified it for my purpose - but it is not working, but as I mentioned it could be the <code>php</code> file</p>
  
  <p>Any help will really mean a lot (even a fix for this will let me eat and sleep properly)</p>
  
  <p>Thanks</p>