2

I read few answers on SO but didn't find the answer for this, is it possible to set ANSI endocing for binary data in php. I'm reading images using curl and echo them to the user, but they are saved as UTF-8, and images are not displayd. When I save it as a file on the server (using fopen fwrite) and open that file everything is ok, the same as if I change encoding to ANSI in Notepad++. Right now I'm testing this on windows but it shouldwork on Unix/Linux machines too. Is it possible to do this in php? This is related to my other question How to display binary data from curl in php which I didn't find solution to.

I try to use mb_detect_encoding function to use with iconv but it don't return anything on localhost where I test it.

I don't know if it need to be Windows-1252 or not, it work on the server which I think should not send different data to different browsers.

Community
  • 1
  • 1
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • 1
    Binary data does not have character encoding, by definition. Only strings have some character encoding! – Basile Starynkevitch Jul 08 '13 at 10:11
  • It is much more likely that you are not retransmitting the file headers saying what type of file it is and what image type etc – Anigel Jul 08 '13 at 10:13
  • And PHP "strings" are binary data, unless you assume they are text in some character encoding. – Joni Jul 08 '13 at 10:14
  • I echo the string and sending header from the server and output data is saved as UTF-8 (when I open it in Notepad++) but I can see it only If I change endoding to ANSI, so I need to force the ANSI somehow. – jcubic Jul 08 '13 at 10:26

1 Answers1

0

Since PHP "strings" are binary data, you first want to encode your string data and then insert that data into the binary data as a whole (which is just a string in PHP).

$string = iconv(mb_detect_encoding($string), 'Windows-1252//TRANSLIT', $string);
tfont
  • 10,891
  • 7
  • 56
  • 52
  • Sorry have no way to test it, don't know where is the code I was using. And one more thing I wanted this for my php proxy I've started this on windows but it was not working, I've started again on Linux and it was working fine. Didn't tested my solution on windows though. – jcubic Feb 14 '19 at 16:11