title: javascript image upload via datauri and php base64 decode
body: <p>I am trying to make an image upload where the JavaScript posts DataURI of an image via AJAX and the PHP receives it to decode it into an image.</p>
  
  <p>The problem is, everything is working fine except that the end product is not an image file.</p>
  
  <p>Please have a look at the following example code.</p>
  
  <p>JavaScript:</p>
  
  <pre><code>dataString='encodedimg='+e.target.result.match(/,(.*)$/)[1]+'&amp;type='+type;
  $.ajax({
    url: 'uploadhandler_ajax.php',
    type: 'POST',
    data: dataString,
    success: function(data){
      //print success message
    });
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>$encodedimg = $_POST['encodedimg'];
  file_put_contents('asdf.png', base64_decode($encodedimg));
  </code></pre>
  
  <p>There is no problem with <code>$_POST['encodedimg']</code> as it produces the right image using online base64 converter. So I am assuming that there is a misuse with <code>file_put_contents()</code> or <code>base64_decode()</code>.</p>
  
  <p>Appreciate the help!</p>