0

I have in base64 encoded string in a $_POST field $_POST['nimage'] if I echo it directly as the src value in an img tag, i see the image just fine in browser: echo "<img src='".$_POST['nimage']."'>";

Now, I'm obviously missing a step, because when I base64_decode the string and write it to a file locally on the server, an attempt to view the created file in browser states error:

"The image 'xxxx://myserversomewhere.com/images/img1.jpg' cannot be displayed because it contains errors"

My decode and file put are:

$file = base64_decode($_POST['nimage']);
file_put_contents('images/'. $_POST['imgname'], $file);

which results in images/img1.jpg on the local server. What am I doing wrong in the decode here? Although the base64 output doesn't appear to be URLencoded I have tried urldecode() on it first before base64_decode() just for safe measure with same results.

First few lines of the base64 encode is:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAF4AqsDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD2gJt+XPJPUGv2A/NB2044oAdtY9M8ccCgB6r8+0jtSYDxEW4xz2qQFCnGOPQ0AAQDJIz9KAF8rI6/hQA9Y+SBgjHIqWA5Yxz2xUsBwUdAMdzSAcFGAB0NADgCVK/KB/OgB6BNzc49agse2OgX2BFZvcCRUO7g

DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • 1
    Are you stripping off the data URI header (`data:image/jpeg;base64,`) before you decode the data? –  Jun 08 '15 at 02:02
  • @HoboSapiens that would be a quick NO... let me try that and hope its just that simple (Sorry, only the 2'nd time i've had to manipulate Base64 encoded data) – DMSJax Jun 08 '15 at 02:10
  • @HoboSapiens didn't help - same error, first 2 lines of data are now `/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAF4AqsDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL` – DMSJax Jun 08 '15 at 02:17
  • Does your code check the return value of the `base64_decode()`? if it doesn't, and the decode has failed, you'll end up with `false`, and the file you have on disk will be very short. Strip the header then try `$result = base64_decode($_POST['nimage']); if ($result === false) {exit("Base64 decode failed");}`. This won't fix your problem, but might identify an issue with the decoding process. It might help to know how you've got the image into your `$_POST` array as well. I'd have expected to handle it through `$_FILES` –  Jun 08 '15 at 02:46
  • @HoboSapiens Man i'm sorry, you were right on stripping the URI Header, while trying to troubleshoot this I used the $_POST field in file_put_contents instead of the decoded string. After I fixed that AND stripped the URI headers it worked just fine. Post it as and answer and thanks. -- You know even searching SO i didn't see anything about stripping the URI header before writing to file... – DMSJax Jun 08 '15 at 02:56

1 Answers1

0

The data you're decoding has a data URI header attached:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...

The header is use by the browser to identify the file type and encoding, but isn't part of the encoded data.

Strip the header (data:image/jpeg;base64,) from the data and base64 decode the rest before writing it to a file: you should be good to go.

$b64 = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...';
$dat = explode(',' $b64);

// element 1 of array from explode() contains B64-encoded data
if (($fileData = base64_decode($dat[1])) === false) { 
   exit('Base64 decoding error.');
}
file_put_contents($someFileName, $fileData);

NB: Check the return value of your call to base64_decode() for false and abort somehow with a message. It will trap any problems with the decoding process (like not removing the header!).

  • Thanks! I used substr() to remove the URI MIME, but explode works better, so i'll make the change. – DMSJax Jun 08 '15 at 03:44