3

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

1
  • You have to be more specific. Give an example of a file you want to send, the command you used, and how it ended up. Commented Jul 25, 2010 at 19:36

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.