1

I am trying to upload an img from an android phone to a php server.

But when it is uploaded its data is lost(the file exist but has 0 bytes)

I dont know if the problem is on the php side or the android side.

Here is the code I use upload

PHP:

<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>

Java:

private  void httpPostFileUpload(String fpath){
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = fpath;
        Log.v("file path",fpath.toString());
        String urlServer = "http://leojg.alwaysdata.net/tests/androidConnect/upload_image.php";
        //String urlServer = "http://65.182.110.63/public/handle_upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        String fname = "";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;

        try
        {
            File f = new File(pathToOurFile);
            fname =  f.getName();

            FileInputStream fileInputStream = new FileInputStream(f);

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            Log.v("ouput stream",outputStream.toString());              

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            Log.d("amanda", "upload async finished, server response : " + serverResponseMessage);

            fileInputStream.close();
            fileInputStream = null;
            outputStream.flush();
            outputStream.close();
            outputStream = null;
            f= null;



            parent.invokeJs("pictureUploadEnded('" + fname + "')");
            //this.delete(fpath);

        }
            catch (Exception ex)
        {
                Log.d("amanda", "exception uploading image : " + ex.getMessage());
        }     
}

Here you can see where the img is stored http://leojg.alwaysdata.net/tests/androidConnect/

SOLUTION:

<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
?>

3 Answers 3

1

You cannot get multipart/form-data from $_REQUEST. Try $_FILES. http://php.net/manual/en/features.file-upload.php

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

3 Comments

I changed but I have the same problem
@leojg - You can't just change $_REQUEST to $_FILES. You have to access the temporary file that PHP uploads (PHP tells you where that temp file is in the $_FILES array). Then, you can use move_uploaded_file to move it where you want it to go.
Thanks. I had to change all the code but now it works. I will post the fix above.
1

Is it a must for you to use PHP upload? I always find FTP upload easier to implement (backend can use FileZilla) and more stable!

2 Comments

Yes it is. I did not know that you could use FTP to upload from android. I will try. Thanks for the advice.
@leojg If i am not mistaken there is no FTP API in Android directly, but you could use the apache one. it works as well. See: stackoverflow.com/questions/1567601/android-ftp-library
0

Try the following method of image loading:

http://loopj.com/android-async-http/

Let me know if it works.

Comments

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.