Save binary data uploaded through AJAX on PHP server
<p>I've read a <code>.png</code> image file into an Array Buffer.</p>

<pre><code>var readSingleFile = function(e) {
          var file = e.target.files[0];
          if (!file) {
            return;
          }
          var reader = new FileReader();
          reader.onload = function(e) {
            var contents =new Int8Array(e.target.result);

            $.ajax({
                   url: 'saveBinaryData.php',
                   type: 'POST',
                   contentType: 'application/octet-stream', 
                   data: contents,
                   processData: false
                }); 
          };
          reader.readAsArrayBuffer(file);
        }
</code></pre>

<p>This is posted to a PHP file as the AJAX describes.</p>

<p>How do I convert this binary data to an image format?</p>

<p>My code so far:</p>

<pre><code>&lt;?php
 if($data=file_get_contents($_POST)){
   echo "Error! Data not read!";
   return;
  }

  $data = imagecreatefromstring($data);

  var_dump($data);

  file_put_contents("recentImage1.png", $data);
  ?&gt;
</code></pre>

<p>These are taken from various solutions found in Google.  The image file is created but does not hold any content and therefore cannot be opened. </p>