3

I am trying to determine a file's mime type. I've tried a few methods, but haven't come up with anything that gives consistent output. I've tried $mime = mime_content_type($file) and $mime = exec('file -bi ' . $file). I am serving up images, CSS, and JavaScript.

Example mime_content_type() output:

  • jquery.min.js - text/plain
  • editor.js - text/plain
  • admin.css - text/plain
  • controls.css - application/x-troff
  • logo.png - text/plain

Example exec(...) output:

  • jquery.min.js - text/plain; charset=us-ascii
  • editor.js - text/x-c++; charset=us-ascii
  • admin.css - text/x-c; charset=us-ascii
  • controls.css - text/x-c; charset=us-ascii
  • logo.png - image/png

As can be seen here, the results are all over the place.

My PHP version is 5.2.6


SOLUTION (thanks to Jacob)

$mimetypes = array(
    'gif' => 'image/gif',
    'png' => 'image/png',
    'jpg' => 'image/jpg',
    'css' => 'text/css',
    'js' => 'text/javascript',
);
$path_parts = pathinfo($file);
if (array_key_exists($path_parts['extension'], $mimetypes)) {
    $mime = $mimetypes[$path_parts['extension']];
} else {
    $mime = 'application/octet-stream';
}
4
  • You said that you were using 5.2.6, how then are you using fileinfo? Commented Mar 16, 2010 at 20:00
  • pathinfo() has been around since PHP 4.0.3 - us.php.net/pathinfo Commented Mar 16, 2010 at 20:21
  • 2
    upvoted for adding solution, that said, the solution provided is inherently insecure as it only checks file extensions and should not be used on uploaded files to check validity. Commented Aug 25, 2010 at 11:23
  • @buggedcom - I agree. In my case, the files are managed by us, so this solution is acceptable. I couldn't find a secure solution that worked. I'd be willing to award an accepted answer for a secure solution that works. Commented Aug 27, 2010 at 14:08

1 Answer 1

4

The Fileinfo extension will do the job, if you're on >= 5.30

  • You should try to avoid having to execute commands
  • mime_content_type is deprecated in PHP 5.30

If unfortunately you are on < 5.30, then I would probably just write it myself, it's a lot more reliable than what you're getting from the above functions/commands.

Here's an example:

<?php
$filename = 'FILENAME HERE';
$mimetypes = array('png' => 'image/png', 'jpg' => 'image/jpg', 'css' => 'text/css',
    'js' => 'application/x-javascript'
    // any other extensions that you may be serving      
);

$ext = strtolower(substr($filename, strrpos($filename, '.') + 1, strlen($filename)));
if(array_key_exists($ext, $mimetypes)) {
    $mime = $mimetypes[$ext];
} else {
    echo 'mime type not found';
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

The Fileinfo is PHP >= 5.3.0. I am on 5.2.6. I just updated my post to reflect this.
Using your solution, but you should change in_array() to array_key_exists() and reverse the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.