tags:

views:

130

answers:

1

Hello all,

This is the synopsis: the PHP code is an interpreter of commands given in a terminal-like fashion, so as a string. Basically PHP receives a string argument, interprets it based on a given regular expression and then execute it. Here, the code receives a string similar to

ftp>fput -file(contents-of-file)

In this case, the code will ftp-fput a string to a given server. Works fine when file is ASCII. Now if the file is binary (ie, an image), the regex will bug and even if it wouldnt I want to encode the binary contents of the file so it can fit into the command string. I then need to be able to decode it on the interpreter side. I've tried base64 encode/decode, hex2bin bin2hex, pack unpack, but the file ftp'd always ends up not being readable by browser. The generated file, when attempted to be opened on Linux, generates an error like

Fatal error reading PNG image file: PNG file corrupted by ASCII-conversion

Any suggestions or clarification requests welcome,

Thanks

+2  A: 

That specific error occurs thanks to smartness on behalf of the PNG developers. They included a carriage return and a newline as part of the standard PNG header, just to catch this problem. It looks like the expected CRLF is being translated to just a LF.

Can you show us the regexes you're using? This really should not happen unless you're expressly converting \r to \n (or discarding it) at some point.

Charles
Right on the spot, Charles. Images with other extensions go smooth sailing. And when I compare the binaries of the original png with the one produced, I can spot the \r\n at the beginning of file. I don't classify this question as answered yet because I'm still working on the regex to get rid of the CRLF.
fabjoa