1

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

3
  • 1
    Is there anything in $_FILES? Commented Mar 19, 2014 at 18:30
  • I made a var_dump($_FILES); which shows file count=0 array(0) { } Commented Mar 19, 2014 at 18:46
  • Are you not using the move_uploaded_file() function? @michaelsmith That is how files are uploaded using PHP. Plus you have a t in $filet in $filet = basename($_FILES['uploaded_file']['tmp_name']); so that could be it. Change it to $file = basename($_FILES['uploaded_file']['tmp_name']); Commented Mar 20, 2014 at 5:15

2 Answers 2

2

Do try the following if you're not already using the move_uploaded_file() function.

That is how files are uploaded using PHP. Plus you have a t in $filet

$filet = basename($_FILES['uploaded_file']['tmp_name']);

so that could be it.

See if this works:

$file_path = "./android/"; // assuming running code from root

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {

    $uploadfile = $uploaddir . $file_path;
    $rec="/android/".$file_path;
    echo $rec;

    echo "success";
} else{
    echo "fail";
}

Also, make sure that the folder has proper write permissions.

Footnotes:

I'm unsure of /android/ and ./android/ one of them may need to be changed in order to reflect the same for each other.

Sign up to request clarification or add additional context in comments.

7 Comments

The php is not the problem. To be absolutely sure, I changed it to your suggestion and I tried with ./android and /android. I also tried on another folder to which uploads work with the iPhone version of the app. All with the same result: the echo is always "fail". The problem must be in the java code, but I have checked and re-checked again and again and can't find what the problem could be. I have also asked our network admin to check if there isn't a firewall rule preventing android to upload files - answer pending
Hey Michael. Ok, that's why I posted in asking if you were using the move_uploaded_file() function. @michaelsmith
Answer from network admin: No firewall problem, no entry in any logfile.
More lost than ever on this one
@michaelsmith: You're facing multiple problems... 1) Your android app is not correctly uploading the file (which is why var_dump($_FILES) is showing 0)... and 2) even if it were uploading a file correctly (verified using the var_dump), you're not actually saving the uploaded file to the server. This is where Fred's answer comes into play as he shows you how to move the TEMP (the "just now uploaded" file) to a KNOWN path (the path you want to save the file). The TEMP file is exactly that... the file that was sent to the server, but stored in a temp directory (usually).
|
0

Finally, it showed that the problem was in the Java code. I deleted the part with the POST and recreated it, and voilà! it works. There might have been an invisible stray symbol somewhere in the connection parameters, go figure.

Thanks to anyone for trying to help.

2 Comments

Right on, always glad to hear a solution was found Michael, cheers and you're welcome. However, did you use that link I provided with the Java code on that page or did you use something else in its place?
I checked that link. What I did at the end was to retype all the code. If I had more time, I would do a compare to see where the difference is. But the next duty is calling already...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.