Uploading file from android (java) to server using PHP

android, java, php

Solution

try multipart entity

public void upload(String filepath) throws IOException
    {
     HttpClient httpclient = new DefaultHttpClient();
     httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     HttpPost httppost = new HttpPost("url");
     File file = new File(filepath);
     MultipartEntity mpEntity = new MultipartEntity();
     ContentBody cbFile = new FileBody(file, "image/jpeg");
     mpEntity.addPart("userfile", cbFile); 
     httppost.setEntity(mpEntity);
     System.out.println("executing request " + httppost.getRequestLine());
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity resEntity = response.getEntity();
             // check the response and do what is required
      }

Problem

Hello Im trying to upload files from my android application to my server using PHP. I have read this posts: How to upload a file using Java HttpClient library working with PHP http://www.veereshr.com/Java/Upload How do I send a file in Android from a mobile device to server using http? This is my JAVA code: ``` public void upload() throws Exception { File file = new File("data/data/com.tigo/databases/exercise"); Log.i("file.getName()", file.getName()); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://***.***.***.***/backDatabase.php"); InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){ // Successfully Uploaded Log.i("uploaded", response.getStatusLine().toString()); } else{ // Did not upload. Add your logic here. Maybe you want to retry. Log.i(" not uploaded", response.getStatusLine().toString()); } httpclient.getConnectionManager().shutdown(); } ``` This is my PHP code: ``` <?php $uploads_dir = '/tigo/databaseBackup'; if (is_uploaded_file($_FILES['exercise']['tmp_name'])) { $info = "File ". $_FILES['exercise']['name'] ." uploaded successfully.\n"; $file = 'emailTest.log'; file_put_contents($file, $info, FILE_APPEND | LOCK_EX); move_uploaded_file ($_FILES['exercise'] ['tmp_name'], $_FILES['exercise'] ['name']); } else { $info = "Possible file upload attack: "; $file = 'emailTest.log'; file_put_contents($file, $info, FILE_APPEND | LOCK_EX); $info = "filename '". $_FILES['exercise']['tmp_name'] . "'."; file_put_contents($file, $info, FILE_APPEND | LOCK_EX); print_r($_FILES); } ?> ``` In my logcat i get HTTP/1.1 200 OK. When i look at the server logs i get this error: ``` PHP Notice: Undefined index: exercise in /var/www/backDatabase.php on line 23 ``` I also tried to use: ``` $_FILES['userfile']['name'] ``` Instead of ``` $_FILES['exercise']['tmp_name'] ``` And i got the same error in my server logs. I think my problem is that I cant get reference to my uploaded file. Thanks for helping.

Original source

Related problems