开发者

PHP Decode Base64, Refresh and Keep data

开发者 https://www.devze.com 2023-03-16 03:57 出处:网络
I have an image that I upload with a HTTP Post method to my webserver written in PHP. My code compresses a bitmap to a jpeg coded byte array and sends that Base64 encoded to the server which decodes,

I have an image that I upload with a HTTP Post method to my webserver written in PHP. My code compresses a bitmap to a jpeg coded byte array and sends that Base64 encoded to the server which decodes, writes it to a file and opens it as a jpg image.

Index.开发者_如何转开发php:

<?php
$base=$_REQUEST['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>';
?>

and I have an empty 0kb test.jpg in the same directory.

Sometimes I can see the image when I quickly press F5(refresh), but then when I refresh again, the image is removed again and it's again 0KB. (working on Google Chrome)

1) How can I make that the picture keeps displaying the data written to test.jpg and doesn't change back to 0kb? So when I view it, the picture is still there. (note that my PHP knowledge is not so good, so instructions would be really much appreciated.)

Upload method in JAVA, Android:

public void uploadFrame(byte[] Frame, String WebClient) {
    String ba1=Base64.encodeBytes(mCurrentFrame);
    ArrayList<NameValuePair> nameValuePairs = new
    ArrayList<NameValuePair>();
       nameValuePairs.add(new BasicNameValuePair("image",ba1));

       try{

           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new
           HttpPost(WebClient);
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           is = entity.getContent();
     }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
     }
}

2) Another question is, if this method is actually safe because people could send any POST Method data to it and display any picture. So how would this work in PHP to first check if a parameter in JAVA is equal to a parameter in your PHP code, so only people with the same parameter could upload this code to his webserver.

This could be a quite intresting topic for some people.

EDIT:

<?php

    if (isset($_REQUEST['image'])) {
        $base=$_REQUEST['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>';
    }

        header('Content-type: image/jpeg');
        readfile('test.jpg');
    ?>


Every time you refresh the page, it's going to write data to that test.jpg file. If you don't submit a base64 image, it'll just write out a blank/null string, which means a 0-byte file.

Instead, try this:

<?php

if (isset($_REQUEST['image'])) {
   ... your decoding/fopen/fwrite stuff here ...
}

header('Content-type: image/jpeg')
readfile('test.jpg');

This will directly output the image as a jpg, rather than outputting HTML and forcing the browser to open yet another HTTP connection to load the .jpg file.


Answering question 2:

No, it is not safe. You should really probably sign it or outright encrypt it in transit to ensure you can trust the data you get -- it's either yours, or garbage. Check out mcrypt() for more info on that.


Quick caveat: if you're setting the header to me a bitmap, dont write HTML to the screen: (echo '<img src=test.jpg>';)

I would echo the header to be image/jpeg and then just write the raw binary to the screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消