Page 1 of 1
MIME TYPES
Posted: Tue Dec 29, 2009 12:04 pm
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?
Re: MIME TYPES
Posted: Tue Dec 29, 2009 12:17 pm
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']);
Re: MIME TYPES
Posted: Tue Dec 29, 2009 1:19 pm
by kendall
I have PHP 5.2 not 5.3
Re: MIME TYPES
Posted: Tue Dec 29, 2009 1:29 pm
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']);
Re: MIME TYPES
Posted: Tue Dec 29, 2009 2:03 pm
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
Re: MIME TYPES
Posted: Tue Dec 29, 2009 2:18 pm
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');
Re: MIME TYPES
Posted: Thu Dec 31, 2009 1:07 am
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.
Re: MIME TYPES
Posted: Thu Dec 31, 2009 6:07 am
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
Re: MIME TYPES
Posted: Thu Dec 31, 2009 6:37 pm
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