MIME TYPES

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

MIME TYPES

Post by kendall »

Hey,

In a $_FILES upload process the $_FILES['type'] for ANY file being uploaded is always "application/octet-stream" Why is this and is there anyway that I can resolve this using php?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: MIME TYPES

Post by AbraCadaver »

The type is set by the browser and you shouldn't rely on it even if it were to appear to be correct. I haven't ever seen it always return application/octet-stream (maybe IE?). One way:

Code: Select all

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $_FILES['file']['tmp_name']);
Or on linux if you don't have the file info extension for PHP:

Code: Select all

$type = exec('file -bi ' . $_FILES['file']['tmp_name']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: MIME TYPES

Post by kendall »

I have PHP 5.2 not 5.3
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: MIME TYPES

Post by AbraCadaver »

Well you can use the exec() version above if on linux. I suppose you tried this already?

Code: Select all

$type = mime_content_type($_FILES['file']['tmp_name']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: MIME TYPES

Post by kendall »

AbraCadaver wrote:Well you can use the exec() version above if on linux. I suppose you tried this already?

Code: Select all

$type = mime_content_type($_FILES['file']['tmp_name']);
Yeah... but mime_content_type comes back empty for some reason....what gives here? I can't use exec() for security reasons
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: MIME TYPES

Post by AbraCadaver »

Not sure. Could be a lot of issues. Maybe it can't determine the type. What does this show?

Code: Select all

echo ini_get('mime_magic.magicfile');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: MIME TYPES

Post by daedalus__ »

if i remember right you are going to have a hell of time with this. it would probably be easier to make sure you serve the files as the correct mime type.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: MIME TYPES

Post by kendall »

daedalus__ wrote:if i remember right you are going to have a hell of time with this. it would probably be easier to make sure you serve the files as the correct mime type.
Please elaborate....I ended up having to use finfo() with a content_mime_type() fall back.... I'm curious to find out though why content_mime_type would return an incorrect mime_type
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: MIME TYPES

Post by daedalus__ »

im not sure myself. i dont know what you are using this for either but if someone uploads an image with .x extension and you serve it as an image/x it wont really matter if it was image/x when it got uploaded it just wont display right or at all. i kinda figured this was about security
Post Reply