1

I have application on Android that send a zip file to PHP server. zip file has multiple pictures inside. What is right method to receive this file stream and make a zip file and extract it.

At the moment i am receiving zip file with this PHP code but it is in .txt format and i can find all pictures names inside but of course pictures are in unreadable format.

$data = file_get_contents("php://input");

if($data === null){
    die ("No data Received");
}



$handler = fopen('new.txt', 'w+');
fclose($handler);
file_put_contents('new.txt',$data );

This is the code on Android side.

FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

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

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

            // Set HTTP method to POST.
            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);

            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();

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        }
        catch (Exception ex)
        {
            //Exception handling
        }
Shah
  • 73
  • 1
  • 1
  • 9
  • easiest way is to base64encode and decode – Illegal Argument May 23 '14 at 12:14
  • but that also increase size and i am avoiding it. Any other suggestion. – Shah May 23 '14 at 12:18
  • How can a zip file be received `in .txt format`? How does that look? – greenapps May 23 '14 at 12:21
  • base64 will increase the size of upload – Illegal Argument May 23 '14 at 12:23
  • Try changing `new.txt` to `new.zip`. Why is it `.txt` ? – MjZac May 23 '14 at 12:24
  • @greenapps let me put android code so it would make sense. – Shah May 23 '14 at 12:25
  • @MjZac i did that and it says damaged format. But if i open txt file with notepad++ and try to find picture names that i have on android phone they are all there. So basically its not proper text file i was trying to check if i am receiving anything. – Shah May 23 '14 at 12:27
  • @IllegalArgument http://stackoverflow.com/questions/11402329/base64-encoded-image-size – Shah May 23 '14 at 12:30
  • You just have to compare the received data with the zipfile sent. Equal filelength? First bytes equal? And so on. Your Android upload code is normal and justs sends the bytes of the file. But what does `file_get_contents("php://input");`? If it reads the whole input then you probably have more then the bytes of the zip. – greenapps May 23 '14 at 12:31
  • shouldnt you be using `$_FILES['uploadedfile']['tmp_name']` to access the file uploaded? – MjZac May 23 '14 at 12:33
  • @greenapps you are right file_get_contents("php://input") dumps whole raw body it has received over http and write it into file. But i have sent long json string and received it with this code and i always received the same output no extra information. So my problem is how to get this information back into zip file. – Shah May 23 '14 at 12:33
  • @MjZac yes that is the normal way. But this new method is very interesting. – greenapps May 23 '14 at 12:35
  • @MjZac This is the first line of this file. "Content-Disposition: form-data; name="uploadedfile";filename="/storage/emulated/0/Pictures/survey.zip"" and what is tmp_name is this file name or full path – Shah May 23 '14 at 12:36
  • @Shah you have it all already in a file. But you have to remove something from the beginnening and maybe something from the end. How does it look that data/file now? Is the begin readable by Wordpad? Ok you just answered that. That begin looks normal. So you have to remove that because you see the multipart format. – greenapps May 23 '14 at 12:37
  • @greenapps here is what it looks like for this file. these are first two lines. --***** Content-Disposition: form-data; name="uploadedfile";filename="/storage/emulated/0/Pictures/survey.zip" PK ìx·D Pictures/survey.zipðffaà Â7Û] @ˆ…8 3¹¤´(µX¿ – Shah May 23 '14 at 12:38
  • The solution is simple: All that is sent by Android you have in that file now. The raw multipart data. So now you have to do the parsing yourself. – greenapps May 23 '14 at 12:40
  • Its not easy to read. I have pictures name i can search through whole document but how am i going to extract one image out of it. ? – Shah May 23 '14 at 12:41
  • You could try to change the Android code to not sent the hyphens and lineends and boundary and content description. Only the bytes of the zipfile. Please try. – greenapps May 23 '14 at 12:43
  • In your android code, while uploading a file, shouldnt we set the `connection.setRequestProperty("ENCTYPE", "multipart/form-data");` – MjZac May 23 '14 at 12:56
  • @greenapps of course you only get contents but what is this useful for how am i going to convert it back into zip file. – Shah May 23 '14 at 13:01
  • @MjZac yes i did set that part as you can see in my android code above. – Shah May 23 '14 at 13:01
  • Nope you haven't, when uploading a file `ENCTYPE` should also be specified. Maybe thats the reason why the zip file is damaged. Please try setting the property and use `.zip` extension. – MjZac May 23 '14 at 13:09
  • @MjZac connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); From above android code. – Shah May 23 '14 at 13:12
  • @ Shah. Then you don't have to convert it back as it is the zip file. Well thats what I think. But please tell how many bytes is your zip file and how many bytes do you receive in $data? – greenapps May 23 '14 at 13:15
  • @greenapps exact same number. Nothing changed. 11.6 MB on android and 11.6 MB on server. – Shah May 23 '14 at 13:19
  • That is not exact. Please answer in the number of bytes. – greenapps May 23 '14 at 13:25
  • @greenapps trust me it is same because i have used this method to get whole databse and save it on mysql and use it everyday. You can try it. About my problem this is not zip file i am receiving. – Shah May 23 '14 at 13:31
  • Well I do not believe you. How many bytes? – greenapps May 23 '14 at 13:33
  • on android = 12175929 on server = 12177408 – Shah May 23 '14 at 13:39
  • How many is it more on server? Please comment!! – greenapps May 23 '14 at 13:40
  • 1479 Calculator does that – Shah May 23 '14 at 13:57
  • Shah I had expected that you would have commented on this. But as you don't I will. Your zip file is contained in this file as I said before. `you have to remove something from the beginnening and maybe something from the end`. But if you do not feel for that then just take a normal php script for a file upload as @MjZac already stated. Such a script has been posted on this forum before and it is also only a few lines. – greenapps May 23 '14 at 14:05
  • @greenapps i have removed everything that was coming with it and still no luck. – Shah May 23 '14 at 14:10
  • How many bytes? But you have to remove not where coming but there were you send. – greenapps May 23 '14 at 14:17

0 Answers0