45
votes

I can't find correct MIME type for TrueType fonts. I need it because I'm using File Uploading Class (CodeIgniter) to upload files, and I want to allow only TTF to be uploaded. Tried this:

'ttf'   =>  'font/ttf'
'ttf'   =>  'font/truetype'

With no success.

Any ideas ?

8

8 Answers

41
votes

TTF font files has the following MIME type: font/ttf.

Before February 2017:

TTF does not have a MIME type assigned. You'll have to use the more general application/octet-stream, which is used to indicate binary data with no assigned MIME type.

21
votes

I've seen font/ttf and application/x-font-ttf used as MIME types for TTF. But if your files are being uploaded as application/octet-stream and you don't want to simply trust the .ttf file extension (or if you want to handle files without an extension), you'll have to check the file content to see whether they're TTF files. The UNIX magic file says that a TTF will begin with the 5 bytes

00 01 00 00 00

(That's 00 01 00 00 from the GDEF table version and the leading 00 from the GlyphClassDef table offset.)

If your file begins with those 5 bytes, it's probably a TTF.

19
votes

As of February 2017, RFC 8081 adds font/* media types, which are also listed in the IANA Media Types list. font/ttf is in this list. Browsers may take some time to catch up.

12
votes

I know this is quite old, but still nobody seems to have provided a concrete example fix. So here we are for future generations:

I had the same problem with Apache2 and Chrome. Chrome would warn that a file sent with the mime-type of application/octet-stream was really a font file - which it was.

The fix for me was to add the following line in my apache2 config file:

AddType application/x-font-ttf .ttf

ps:

I had tried to update the magic file but that failed to work after full apache2 reloads. The matches I tried (using real tab characters between fields, and per the above referenced magic patterns) are below:

  # True Type fonts
  0 string  \000\001\000\000\000  application/x-font-ttf
  0 string  \000\001\000\000\000  TrueType font data mime  application/x-font-ttf
5
votes

I've no experience with codeigniter but I tend to believe the correct mimetype is:

application/x-font-ttf

I'm not sure if this solves your problem

4
votes

There is now a media type for ttf and otf.

See: https://www.iana.org/assignments/media-types/media-types.xml#font

ttf is font/ttf

otf is font/otf

woff is font/woff

and woff2 is font/woff2

3
votes

Time to do some debug! If something is not working, the best option is to crack open the code and get your hands dirty.

Open up the Uploads library (system/libraries/Upload.php) and look for this chunk of code around line 200:

    // Set the uploaded data as class variables
    $this->file_temp = $_FILES[$field]['tmp_name'];
    $this->file_size = $_FILES[$field]['size'];
    $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
    $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
    $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
    $this->file_ext  = $this->get_extension($this->file_name);
    $this->client_name = $this->file_name;

You'll see that $this->file_type is being set there. var_dump() that and see what it contains.

The chances are you have some obscure MIME type that CodeIgniter does not know about. Put that MIME type into config/mimes.php and it should work fine.

When you have it working and accept this answer please comment with the MIME type you used and I will add it to CodeIgniter Reactor to make sure nobody else gets stuck.

3
votes

Can you try this:

application/font-sfnt

As I can see in iana, the correct header is this for TTF: http://www.iana.org/assignments/media-types/application/font-sfnt