This entry is the first of three parts covering streams in PHP. I’m preparing for ZCE certification and streams are important part to pass the exam. At least I think so…
Now to the point. By using stream contexts we can „hook up” to stream notifications by
stream_context_set_params function. When you want to know what’s happening during stream processing live, the best way is to use stream context notification param. Take a look at the simplest snippet:
Notice that even when we didn’t start fetching stream contents yet, a few interesting things happened:
- stream has connected,
- retrieved mime type,
- redirect took place (not always),
- retrieved file size (not always),
- notified 0 bytes progress.
Also notice that this method won’t work with local resources (at least on my server), what brings us to second method involving stream reading:
This method will always work under 2 conditions:
- you fetched enough bytes (for eg. Adobe PDF full recognition needs sometimes about 10 bytes),
- you check mimetype with first pass (it’s obvious) or you end up with octet stream generics.
Stay tuned for second part about custom stream filters.