0

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?

Daniel Marín
  • 1,369
  • 14
  • 36

2 Answers2

0

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

Community
  • 1
  • 1
Samy Massoud
  • 4,295
  • 2
  • 35
  • 48
  • Still the same error: 'Can't display the image because it contains errors.' – Daniel Marín May 05 '14 at 10:03
  • The thing is, if I remove the header function, the browser shows the stream. That stream is different that the one I get in the web app. – Daniel Marín May 05 '14 at 10:14
  • I've edited my initial question, please take a look at it. By the way, I'm using IIS, not Apache, and I think that maybe could be some problem with the charset configuration or something like that. Thanks. – Daniel Marín May 05 '14 at 10:21
  • 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 . – Samy Massoud May 05 '14 at 10:35
  • I did what you said, but still the same error. Again, I dont think it's a problem with the header, because if I don't print any header I still get a wrong stream. – Daniel Marín May 05 '14 at 10:49
  • i will attach file to you please test it on your server and give me result back , Ok – Samy Massoud May 05 '14 at 10:50
  • I'm not using a FTP. I'm coding directly on the server since I have access to the server machine by TeamViewer. – Daniel Marín May 05 '14 at 10:52
  • Please upload this file as it's to your machine and test http://ge.tt/3FtGRuf1/v/0 – Samy Massoud May 05 '14 at 10:53
  • That code works. I don't know where is my problem then. – Daniel Marín May 05 '14 at 11:16
  • it's HEADER problem :D believe me just fix your file or use my file as base to your code, also you can accept this answer i will modify it – Samy Massoud May 05 '14 at 11:42
  • I commented your "header()..." line and I got a valid stream. I did the same in my file and I got an invalid (and different) stream. I'm not using headers. – Daniel Marín May 05 '14 at 11:58
  • Take a look at "Edit 2" in the original question. Thanks. – Daniel Marín May 05 '14 at 13:08
  • Solved! I was including a "config.php". I changed the char codification of that file to UTF-8 Without bom and it worked! Thank you very much for your ideas. – Daniel Marín May 05 '14 at 13:16
0

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

Loopo
  • 2,204
  • 2
  • 28
  • 45