Page 1 of 1
MIME Content Types
Posted: Sun Jun 20, 2004 8:37 pm
by protokol
Any ideas how to get the mime-type of a file if shell_exec() cannot be called and mime_content_type() is unavailable?
This does NOT work on the server I'm using:
Code: Select all
<?php
if (!function_exists('mime_content_type')) {
$filename = $this->path.$_FILES['file']['name'];
$mime_type = `file -bi "$filename"`;
} else {
$mime_type = mime_content_type($this->path.$_FILES['file']['name']);
}
?>
Posted: Sun Jun 20, 2004 9:12 pm
by John Cartwright
what r the errors..
Posted: Sun Jun 20, 2004 10:01 pm
by redmonkey
You should consider using
escapeshellcmd() when dealing with user input this way.
You could just extract the file extension from the filename and have a wild guess on the mime type based on that (not very accurate). Also, some browsers pass the mime type with the rest of the file data but again this is also not very accurate.
From what I gather though, mime_content_type just grabs the file extension and looks it up in the 'magic.mime' file (just a guess though) so presumably using my first suggestion should yeild similar results.
Posted: Tue Jun 22, 2004 12:40 am
by protokol
Yeah, looks like I will unfortunately not be able to call the UNIX 'file' command to get the MIME-type. So it appears that the only alternative is to create an array with known MIME-types based on file extensions in order to gather this information.
Oh well, although it's not the most reliable way, at least it's better than nothing.
Posted: Tue Jun 22, 2004 12:42 am
by Illusionist
what about just $_FILES['file']['type']
Posted: Tue Jun 22, 2004 12:42 am
by protokol
redmonkey wrote:You should consider using
escapeshellcmd() when dealing with user input this way.
Yes, before I call these functions, I do this. I didn't list it though because it's irrelevent to the problem at hand.
Code: Select all
<?php
$_FILES['file']['name'] =& escapeshellcmd($_FILES['file']['name']);
?>
Posted: Tue Jun 22, 2004 12:44 am
by protokol
Illusionist wrote:what about just $_FILES['file']['type']
Because my dumbass didn't even think to use that.
Posted: Tue Jun 22, 2004 12:53 am
by Illusionist

it's all good!