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

[email protected] (Ashley Sheridan)
Newsgroups php.general
Message-ID <1268064851.3078.58.camel@localhost>
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.
> 
> rday
> --
> 
> ========================================================================
> Robert P. J. Day                               Waterloo, Ontario, CANADA
> 
>             Linux Consulting, Training and Kernel Pedantry.
> 
> Web page:                                          http://crashcourse.ca
> Twitter:                                       http://twitter.com/rpjday
> ========================================================================
> 


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.

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!

Having said that, I've had good results from using "file -f filename" on
Linux, which is using version 5.03 on my system.

Thanks,
Ash
http://www.ashleysheridan.co.uk
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.