android multipart post to php
<p>i was trying to upload a file from my android device to a php server, but i get "Notice:  Undefined index: file in upload_file.php on line 3. Any sugestions what this could be?</p>

<p>This is the upload_file.php</p>

<pre><code>&lt;?php
if(isset($_POST))
{
  $allowedExts = array("jpg", "jpeg", "gif", "png");
  $extension = end(explode(".", $_FILES["file"]["name"]));
  echo $_FILES["file"]["type"] . ($_FILES["file"]["size"] &lt; 2000000);
  if (((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    &amp;&amp; ($_FILES["file"]["size"] &lt; 2000000))
    &amp;&amp; in_array($extension, $allowedExts))
  {
    if ($_FILES["file"]["error"] &gt; 0)
    {
      echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
    }
    else
    {
      echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;";
      echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;";
      if (file_exists("files/" . $_FILES["file"]["name"]))
      {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
      else
      {
        move_uploaded_file($_FILES["file"]["tmp_name"],"files/" . $_FILES["file"]["name"]);
        echo "Stored in: " . "files/" . $_FILES["file"]["name"];
      }
    }
  }
  else
  {
    echo "Invalid file";
  }
}
else
{
  echo "nothing posted";
}
?&gt;
</code></pre>

<p>and this is gets executed on my AndroidDevice</p>

<pre><code>/**
 * POSTphoto
 **/
private class POSTphoto
        extends AsyncTask&lt;String,String,String&gt;
{
    @Override
    /** doInBackground */
    protected String doInBackground( String... filename )
    {
        File file = new File( filename[0] );
        HttpPost request;
        MultipartEntity mpe = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE );
        FileBody cbFile;
        try
        {
            cbFile = new FileBody( file , "image/jpeg" );
            mpe.addPart( "file" , cbFile );

            // Creating the request.
            String resource = APIcontroller.buildUrl( "" );
            request = new HttpPost( resource );
            request.addHeader( "Content-Type" , "multipart/form-data" );
            request.setEntity( mpe );
            return APIcontroller.execute( request );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            return "some error";
        }
    }// doInBackground

    @Override
    /** Shows the content of the result.
     * @param result is what the server returns in doInBackground.
     **/
    protected void onPostExecute( String result )
    {
        // CLose clients connection pool.
        APIcontroller.closeClient();
        if ( !result.equals( "" ) )
        { // Result not empty.
            Log.d( "result" , result );
            Toast.makeText( FilebrowserActivity.this , result ,
                    Toast.LENGTH_LONG ).show();
        }
        else
        { // Result empty.
            Toast.makeText( FilebrowserActivity.this , "FAILED" ,
                    Toast.LENGTH_LONG ).show();
        }
    } // onPostExecute
} // POSTphoto
</code></pre>
