Save base64 encoded image into the file in PHP
<p>I have the following code : </p>

<pre><code>&lt;?php
header('Content-Type: image/png');
$data = "iVBORw0KGgoAAAANSUhEUgAAAuAAAAI8CAYAAACwIh5dAAAgAElkZJgRCXxOAuIPzzTlg......";       
define('UPLOAD_DIR', '/home/Desktop/image.png');
$img = $data;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?&gt;
</code></pre>

<p>Here <code>$data</code> contain the encoded string of image in base-64. I want to save the image into the file. I did the previous code also go through lot of tutorial in google but it's not working. </p>

<p>Can anyone help me where I am going wrong.</p>

<p>Thanks in advance.</p>
