PHP Decode Base64, Refresh and Keep data
<p>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.</p>

<p>Index.php:</p>

<pre><code>&lt;?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 '&lt;img src=test.jpg&gt;';
?&gt;
</code></pre>

<p>and I have an empty 0kb test.jpg in the same directory.</p>

<p>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)</p>

<p>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.)</p>

<p>Upload method in JAVA, Android:</p>

<pre><code>public void uploadFrame(byte[] Frame, String WebClient) {
    String ba1=Base64.encodeBytes(mCurrentFrame);
    ArrayList&lt;NameValuePair&gt; nameValuePairs = new
    ArrayList&lt;NameValuePair&gt;();
       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());
     }
}
</code></pre>

<p>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.</p>

<p>This could be a quite intresting topic for some people.</p>

<p>EDIT:</p>

<pre><code>&lt;?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 '&lt;img src=test.jpg&gt;';
    }

        header('Content-type: image/jpeg');
        readfile('test.jpg');
    ?&gt;
</code></pre>
