Tag Archives: english

webmastering

Writing custom stream filters

Perhaps you used once or twice buil­tin zlib.* or mcrypt.* stream filters. Have you tried making your own stream filter? Maybe you didn’t need to. There are ways to accom­plish busi­ness needs without making hands dirty by not well docu­men­ted stuff like user filters. I need to know that topic like I said before, because of certification :-)

We’re going to make stream filter capa­ble of compu­ting hash check­sum from stream contents on the fly. Rich docu­men­ted code below:

webmastering

2 ways to check stream mimetype

This entry is the first of three parts cove­ring stre­ams in PHP. I’m prepa­ring for ZCE certi­fi­ca­tion and stre­ams are impor­tant part to pass the exam. At least I think so…

Now to the point.  By using stream conte­xts we can „hook up” to stream noti­fi­ca­tions by stream_context_set_params func­tion. When you want to know what’s happe­ning during stream proces­sing live, the best way is to use stream context noti­fi­ca­tion param. Take a look at the simplest snip­pet:

Notice that even when we didn’t start fetching stream contents yet, a few inte­re­sting things happened:

  • stream has connected,
  • retrie­ved mime type,
  • redi­rect took place (not always),
  • retrie­ved file size (not always),
  • noti­fied 0 bytes progress.

Also notice that this method won’t work with local reso­ur­ces (at least on my server), what brings us to second method invo­lving stream reading:

This method will always work under 2 conditions:

  • you fetched enough bytes (for eg. Adobe PDF full reco­gni­tion needs some­ti­mes about 10 bytes),
  • you check mime­type with first pass (it’s obvious) or you end up with octet stream generics.

Stay tuned for second part about custom stream filters.

webmastering

Playing with PHP functional way

I’m still inspi­red by Scala way of thin­king. I wonder if some func­tio­nal para­digms can be smug­gled into PHP. My goal is to quic­kly code flexi­ble string conver­ter without using loops or if’s (ternary opera­tors don’t count ;-) ). I’ll be using func­tions that take other func­tions as their argu­ments, which are known as higher order func­tions in Scala. Of course, in Scala you can force what kind of argu­ments func­tion takes, and what it returns. In PHP you can use calla­ble type­hint and tedious reflec­tion processing.

Enough talking, gimme some code!