Question

I'm trying to show a jpg that was previously encoded in a WCF web service using:

<?php
require_once '../inc/config.php';
[...]
header("Content-type: image/jpg");
echo base64_decode($doc['BDATA']);

But I'm getting a

Can't display the image because it contains errors.

I've decoded the base64 string in this web app www.opinionatedgeek.com/dotnet/tools/base64decode/ and the result is right, but different that the one I'm getting with base64_decode, which is wrong.

Edit: I have two enviroments using the same code: Test and Production. It works fine in Test, but not in Production, so I'm thinking in some configuration problem.

I'm working with PHP 5.5.9 in Microsoft IIS.

An example of a string that base64_decode isn't decoding well: /9j/4AAQSkZJRgABAQEAeAB4AAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9/KKKKAP/2Q==

Any ideas?

Edit 2: If I comment this line require_once '../inc/config.php'; and copy the code from config.php to my actual file, it works fine. What could be happening?

Was it helpful?

Solution

From base_64_decode manual comments

php <= 5.0.5's base64_decode( $string ) will assume that a space is meant to be a + sign where php >= 5.1.0's base64_decode( $string ) will no longer make that assumption

To fix this behavior try this code

$encodedData = str_replace(' ','+',$encodedData);
$decocedData = base64_decode($encodedData);

As this is no't your case then you have to check this answer
Because every thing work fine for me here on (WAMP)

EDIT: As in our below conversation

There are a lot of things that may corrupt header for example , if your file encoding is UTF-8 then you should save it as UTF-8 Without bom you can do this using notepad ++ , also make sure if you use FTP that your client didn't any chars to your file , rather than that every thing should work fine

OTHER TIPS

base64 encoding is not completely standardised.

Some implementations use different characters, so you'll have to replace those characters before you run your decode.

further details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top