'file --mime-type' vs 'mimetype' for php files

I'm trying to understand the difference between file --mime-type and mimetype. They are giving me different results on Ubuntu 12.04.4 LTS for this simple php file.

<?php echo 'Hello world!';
?>

Now when I use mimetype and file to get the type of the file, I get:

$ mimetype -b test.php
application/x-php
$ file -b --mime-type test.php
test/x-php

This question ('file --mime-type' and 'mimetype' Commands Returning Different Results) says that file uses /etc/mime.types however:

$ grep php /etc/mime.types
application/x-httpd-php phtml pht php
application/x-httpd-php-source phps
application/x-httpd-php3 php3
application/x-httpd-php3-preprocessed php3p
application/x-httpd-php4 php4
application/x-httpd-php5 php5

I'm most curious about why file sees it as a text file. This is causing me problems in Rails as Ruby's MIME type also sees it as type application.

1 Answer

According to the manpage file doesn't use /etc/mime.types but the compiled definitions from /usr/share/misc/magic.mgc, the plain text definitions from /etc/magic and some other files.

You can get the source code for /usr/share/misc/magic.mgc from the source code of the file package (have a look into the magic/ subfolder).

If you want file to return application/x-php add the following to /etc/magic:

# PHP scripts
# Ulf Harnhammar <>
0 search/1/c =<?php PHP script text
!:mime application/x-php
0 search/1 =<?\n PHP script text
!:mime application/x-php
0 search/1 =<?\r PHP script text
!:mime application/x-php
0 search/1/w #!\ /usr/local/bin/php PHP script text executable
!:mime application/x-php
0 search/1/w #!\ /usr/bin/php PHP script text executable
!:mime application/x-php
# Smarty compiled template,
# Elan Ruusamäe <>
0 string =<?php\ /*\ Smarty\ version Smarty compiled template
>24 regex [0-9.]+ \b, version %s
!:mime application/x-php
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like