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.

Podobne wpisy:

  1. Writing custom stream filters
  2. Using PHP traits to check class constants values
  3. Using stre­ams in multi process environment
  4. Multi­th­re­ading in web brow­ser (Web Workers + jQuery)

2 Comments

  • zordon
    21 marca 2014 - 09:27 | Permalink

    Power­ful yet still little-known part of php program­ming. I’m very inte­re­sted in conti­nu­ation and more complex real-life exam­ples.
    And good luck with the certification!

  • 24 marca 2014 - 23:36 | Permalink

    Thx zordon.
    You wanted — you have it!
    http://spiechu.pl/2014/03/24/writing-custom-stream-filters

  • Dodaj komentarz

    Twój adres e-mail nie zostanie opublikowany.

    Możesz użyć następujących tagów oraz atrybutów HTML-a: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>