title: Code to upload Video from Android to PHP
body: <p>I am trying to upload video from Android code to my PHP web server code but can't get success. I am referring the following <a href="http://reecon.wordpress.com/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/" rel="nofollow">link</a> to perform the uploading task but I am getting the following response in my Android code, but can't found any file on PHP server.</p>
  
  <p>Android response</p>
  
  <pre><code>DEBUG/ServerCode(29484): 200
  DEBUG/serverResponseMessage(29484): OK
  </code></pre>
  
  <p>I have checked many things like setting values in <code>php.ini</code> files.
  Although I can upload images from Android code to server becasue from Android I am sending a 64 bit encoded <code>ByteArray</code> and therer is a ready-made function in php which can create image from encoded <code>ByteArray</code>s.</p>
  
  <p>But that code also is not working in case of any other file having type other than image.  </p>
  
  <p>Please guide me if any of you have done something similar before.  </p>
  
  <p>PHP code I am using: </p>
  
  <pre><code>&lt;?php
  
      $target_path  = "./upload/";
  
      $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!";
      }
  ?&gt;
  </code></pre>
  
  <p>Android code I am using:</p>
  
  <pre><code>public void videoUpload()
  {
      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;
      DataInputStream inputStream = null;
  
  
      String pathToOurFile = "/sdcard/video-2010-03-07-15-40-57.3gp";
      String urlServer = "http://10.0.0.15/sampleWeb/handle_upload.php";
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary =  "*****";
  
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1*1024*1024;
  
      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");
  
      outputStream = new DataOutputStream( connection.getOutputStream() );
      outputStream.writeBytes(twoHyphens + boundary + lineEnd);
      outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile );
      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)
      int serverResponseCode = connection.getResponseCode();
       String serverResponseMessage = connection.getResponseMessage();
       Log.d("ServerCode",""+serverResponseCode);
       Log.d("serverResponseMessage",""+serverResponseMessage);
      fileInputStream.close();
      outputStream.flush();
      outputStream.close();
      }
      catch (Exception ex)
      {
          ex.printStackTrace();
      }
  }
  </code></pre>