title: Uploading Files using PHP Script to Web Hosting
body: <p>I have written out a program which is designed to upload text files created from motion data information to a PHP cloud server that I have set up with 000webhost.com. Ive tried running it however it will not run for some reason. None of the sensor information is uploaded and there seems to be a problem me thinks on the server end. Is the error in the script or code? (or both?). </p>
  
  <p>Edit: Yeah I know its not "great", I am still fairly new to coding and been trying to fiddle around with the code for a while (still working on it).</p>
  
  <p>Program</p>
  
  <pre><code>package com.example.motionsense3;
  
  import java.io.FileOutputStream;
  import java.io.FileInputStream;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.TimerTask;
  import android.content.Context;
  import android.os.Bundle;
  import android.widget.Toast;
  import java.net.URL;
  import java.net.HttpURLConnection;
  import java.io.DataOutputStream;
  import java.io.File;
  import android.util.Log;
  
  
  public class SaveTimer extends TimerTask
  {
      String upLoadServerUri = null;
      final String uploadFilePath = "/data/data/com.example.motionsense3/";
      //final String uploadFileName = "????";
  
      private ArrayList&lt;String&gt; motionData;
      private ArrayList&lt;String&gt; motionDataClone;
      private Context context;
  
      public SaveTimer(ArrayList&lt;String&gt; motionData, MainActivity context) {
          this.motionDataClone = (ArrayList&lt;String&gt;) motionData.clone();
          this.motionData = motionData;
          this.context = context;
      }
  
      @Override
      public void run() {
          Log.e("DIDUPLOADWORK", Boolean.toString(upload()));
  
      }
  
      private void save() {
           try {
               this.motionDataClone = (ArrayList&lt;String&gt;) motionData.clone();
               motionData.clear();
  
               FileOutputStream fileOutput = context.openFileOutput("scaninfo_" + new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss").format(new Date()).toString(), context.MODE_WORLD_READABLE);//.write(s);
               for(String s : motionDataClone)
               {
                   fileOutput.write(s.getBytes());
               }
               fileOutput.close();
  
  
           } catch (Exception e) {
  
           }
  
          //save();
  
  }
  
  
           public boolean upload(){//ArrayList&lt;String&gt; motion){//String sourceFileUri) {
  
               /************* Php script path ****************/
               upLoadServerUri = "motionsense.uphero.com/public_html/motiondata/UploadToServer.php";
  
            //String fileName = sourceFileUri;
  
            HttpURLConnection conn = null;
            DataOutputStream dos = null;  
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024; 
  
  
                 try { 
  
                     URL url = new URL(upLoadServerUri);
                      String fileName = "scaninfo_" + new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss").format(new Date()).toString();
                     // Open a HTTP  connection to  the URL
                     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);
  
  
                     for(String s : motionDataClone)
                     {
                         dos.write(s.getBytes());
                     }
  
                     // send multipart form data necesssary after file data...
                     dos.writeBytes(lineEnd);
                     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
                     // Responses from the server (code and message)
                     int serverResponseCode = conn.getResponseCode();
                     String serverResponseMessage = conn.getResponseMessage();
                     Log.e("SERVERRESPONSE", serverResponseMessage);
                     Log.e("SERVERRESPONSECODE", String.valueOf(serverResponseCode));
  
                 }
                 catch(Exception e){return false;}
                 return true;
           }
      }
  </code></pre>
  
  <p>Php script</p>
  
  <pre><code>&lt;?php
  
      $file_path = "motiondata/";
  
      $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>