Re: [PHP] best way to determine (MIME) content type of a stream of bytes?

[email protected] ("Robert P. J. Day")
Newsgroups php.general
Message-ID <alpine.LFD.2.00.1003081133130.8061@localhost>
On Mon, 8 Mar 2010, Ashley Sheridan wrote:

> On Mon, 2010-03-08 at 10:57 -0500, Robert P. J. Day wrote:
>
> > hi, i'm interested in the most comprehensive way to determine the
> > content type of a stream of bytes that's been uploaded to a PHP
> > script?  assuming that the bytes are uploaded simply via a POST
> > parameter, i can see that there are a couple ways to do it:
> >
> >   * getimagesize()
> >   * FileInfo
> >
> > i've been doing some testing this morning and a few video formats
> > handed to FileInfo come back as "application/octet-stream" which
> > isn't particularly informative.  and i want to support as many
> > different formats of image, audio and video as possible.
> >
> >   so ... what's the best way?  oh, by the way, when i used
> > fileinfo, i didn't bother handing over a magic file.  i'm starting
> > to think that would make a difference.  and is there a noticeable
> > advantage to upgrading to PHP 5.3 since the server (centos 5.4) is
> > currently running only PHP 5.1.6.  thanks.

> If you're wanting to grab details about a clip, what about using
> mplayer for dealing with video clips. It has more than a few command
> line options that can return various levels of detail about a media
> file. You could use the extension of the clip as a hint about what
> way you can determine a files exact type. So, if a file came in with
> a jpg, png or gif extension, you could use GD functions to determine
> if it's really an image. If it's a .avi, .mpg, .mp4, .mp3, .ogg, you
> could use mplayer to deal with it.

  in order to make life as difficult as possible, all i can assume is
an incoming stream of bytes.  i will have no idea where it came from,
or its original file name.  all of the mime/type identification has to
be done by the PHP script on the server end, based solely on the
content.  (i'm fairly sure that means a "magic" file will have to be
involved.)

> This does seem to ba a bit of an area where PHP is lacking. Even the
> manual pages are cryptic. It seems to suggest that the Mime
> functions which we should use in-place of deprecated ones themselves
> rely on those same deprecated functions!

  i have noticed that.  the "mime_content_type()" function looked like
a good candidate but it's marked as deprecated.  the best option
appears to be the Fileinfo stuff.

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.