How can I read few bytes from posted data?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wvxvw
Forum Newbie
Posts: 22
Joined: Sat May 17, 2008 10:55 am

How can I read few bytes from posted data?

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post by Benjamin »

More than likely substr() is binary safe.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

I would suggest the fileinfo PECL extension, which already does exactly what you are looking for - http://pecl.php.net/package/Fileinfo
wvxvw
Forum Newbie
Posts: 22
Joined: Sat May 17, 2008 10:55 am

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

Post 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.
Post Reply