Page 1 of 1

How can I read few bytes from posted data?

Posted: Fri Jul 11, 2008 8:45 am
by wvxvw
Hi.
What I'm trying to do, is to read the file header, and than to decide what to do with it. E.g. if it says it's 3 first bytes are \xEF \xBB \xBF, the script will treat it as if it was a UTF-8 encoded text, and if it's \x47 \x49 \x46, than it's a GIF image etc.
The problem is I'm receiving the file via $GLOBALS["HTTP_RAW_POST_DATA"], and I have no clue to how can I extract bytes from it...
Suggestions / manual links / tutorial will be much apprecated.

Re: How can I read few bytes from posted data?

Posted: Fri Jul 11, 2008 6:01 pm
by Benjamin
More than likely substr() is binary safe.

Re: How can I read few bytes from posted data?

Posted: Fri Jul 11, 2008 6:04 pm
by Eran
I would suggest the fileinfo PECL extension, which already does exactly what you are looking for - http://pecl.php.net/package/Fileinfo

Re: How can I read few bytes from posted data?

Posted: Sat Jul 12, 2008 4:39 am
by wvxvw
Thank you.
I actually ened up using unpack() and making some substitute table, but PECL extension should be probably a better way to do it, I'll have to check it more thoroughly.