MIME Content Types

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

MIME Content Types

Post 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']);
}

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what r the errors..
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

what about just $_FILES['file']['type']
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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']);
?>
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Illusionist wrote:what about just $_FILES['file']['type']
Because my dumbass didn't even think to use that.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

:-D :wink: it's all good!
Post Reply