I am trying to upload a .png file to my server. The php gets called and returns, but the file is never uploaded. I must have some glitch in my code, but I just can't find it.
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if(myBitmap!=null){
pixels = new byte[myBitmap.getWidth() * myBitmap.getHeight()];
for (int i = 0; i < myBitmap.getWidth(); ++i) {
for (int j = 0; j < myBitmap.getHeight(); ++j) {
pixels[i + j] = (byte) ((myBitmap.getPixel(i, j) & 0x80) >> 7);
}
}
}
HttpURLConnection connectionWWW = (HttpURLConnection) url.openConnection();
connectionWWW.setChunkedStreamingMode(0);//3.0
connectionWWW.setReadTimeout(100000);
String boundary = "*****";
String attachmentFileName = "screenshot.png";
String crlf = "\r\n";
String twoHyphens = "--";
if(imgFile.exists()){
connectionWWW.setUseCaches(false);//3.0
connectionWWW.setDoOutput(true);
connectionWWW.setDoInput(true);
connectionWWW.setRequestMethod("POST");
connectionWWW.setRequestProperty("Connection", "Keep-Alive");
connectionWWW.setRequestProperty("ENCTYPE", "multipart/form-data");
connectionWWW.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connectionWWW.setRequestProperty("uploaded_file", attachmentFileName);
DataOutputStream request = new DataOutputStream(connectionWWW.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + attachmentFileName + "\"" + crlf);
request.writeBytes(crlf);
request.write(pixels);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close();
}
in = new BufferedInputStream(connectionWWW.getInputStream());
String res = convertStreamToString(in);
connectionWWW.disconnect();//3.0
The php:
$uploaddir = './android/';
$file = basename($_FILES['uploaded_file']['name']);
$filet = basename($_FILES['uploaded_file']['tmp_name']);
$uploadfile = $uploaddir . $file;
$rec="/android/".$file;
echo $rec;
$rec always answers "./android" - $file is empty
$_FILES?move_uploaded_file()function? @michaelsmith That is how files are uploaded using PHP. Plus you have atin$filetin$filet = basename($_FILES['uploaded_file']['tmp_name']);so that could be it. Change it to$file = basename($_FILES['uploaded_file']['tmp_name']);