开发者

Converting binary to string then back to binary

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜