i am developing a android project i need to upload a bitmap on the apache server using a php script method i am using is as follows
public void doFileUpload(Bitmap bitmapOrg) {
InputStream is;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1= Base64.encodeToString(ba, 0);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://172.17.0.250/upload.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.print(entity.toString());
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
}
and i donot know anything about php. i found this script for handling the file as follow.
<?php
$base=$_FILES['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>
connection is getting established but i cant see any file in the /var/www/html/ directory. secondly i am not getting any message in the logcat. i dont know what to do. i am a newbie to android and dont know anything about php. i understand java and core concepts of c, bash.kindly help me with the php scipt.
edit 1
as per the comments i have changed the php script to
<?php
$file_path = "uploads/";
$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";
}
?>
and modified the method as
HttpEntity entity = response.getEntity();
is = entity.getContent();
System.out.println(is.read());
it is showing 102 in the log cat