51

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 ?

Robin Kanters
  • 5,018
  • 2
  • 20
  • 36
CappY
  • 1,510
  • 1
  • 17
  • 32

8 Answers8

49

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.

Sergei Kuraksin
  • 772
  • 9
  • 10
Ori
  • 4,132
  • 1
  • 25
  • 27
  • how to use it do identify if file is TTF ? Thx – CappY Feb 26 '11 at 18:00
  • check the extension? let me know if you need the code for that. i don't think you really can do much more security check than that. – Aditya M P Feb 27 '11 at 03:54
  • 1
    Chrome annoyingly throws this warning for every single .ttf file it sees: Resource interpreted as Font but transferred with MIME type application/octet-stream. – Chris Moschini May 10 '11 at 20:30
  • 6
    @ChrisMoschini that gets solved using application/x-font-ttf instead – cprcrack Aug 22 '13 at 19:12
  • @cprcrack Tested on IIS7.5 and you are correct, changing .ttf to serve as application/x-font-ttf fixed the Chrome issue. I see dkarp's answer suggests that, but when I commented his answer wasn't even here to see - it's too bad StackOverflow can't notify me of rising answers on specific questions, especially ones I've commented on. – Chris Moschini Aug 23 '13 at 00:26
21

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.

Community
  • 1
  • 1
George Helyar
  • 4,319
  • 1
  • 22
  • 20
21

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.

dkarp
  • 14,483
  • 6
  • 58
  • 65
  • 4
    @aditya: Well, yeah. It's a heuristic. If the OP *really* wants to be sure, he can write a full validating TTF parser. But checking for a leading hex `00 01 00 00 00` is the standard way to detect a TTF file from its contents. Put those 5 bytes in a file `foo` and call `file foo` from a UNIX/Mac prompt, and the `file` command will respond "`foo: TrueType font data`". – dkarp Feb 27 '11 at 15:51
  • i was just trying to act smarter than i actually am. thanks for baking up the humble pie. :D – Aditya M P Mar 01 '11 at 09:49
12

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
Michael Mikowski
  • 1,269
  • 1
  • 10
  • 21
6

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

tchap
  • 3,412
  • 3
  • 29
  • 46
vorticist
  • 61
  • 1
  • 2
5

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

rene
  • 41,474
  • 78
  • 114
  • 152
  • Not working. :( Well in CI there is file config/mimes.php containing array with types. – CappY Feb 26 '11 at 16:42
  • It is "correct" in that (something like) this is what you use when there is no official designated MIME type. It "doesn't work" in the sense that because there is no official spec, you cannot expect receiving applications to know what to do with it. But from a MIME perspective, this is the way to go, and if you find that this is a frequent need, approach the IANA with a formal request to register a proper MIME type for this data type. See further http://www.iana.org/assignments/media-types/index.html – tripleee Sep 06 '12 at 08:29
4

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

Vasilis Vasilatos
  • 750
  • 10
  • 11
3

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.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
  • @Phil - It's application/octet-stream but it's same for .EXE Any way for recognizing as TTF or I should use application/octet-stream and hope user's submit TTFs ? :) – CappY Feb 26 '11 at 19:22
  • 1
    Some browsers just screw these things up. It checks the extension too which is of course not 100% reliable, but who cares if they upload a .exe as a .rtf? It won't execute properly anyway. – Phil Sturgeon Feb 26 '11 at 21:07
  • 1
    Well....maybe the only solution is to upload file, then check the filename.TTF,and if there isn't TTF to delete it. – CappY Feb 26 '11 at 23:12
  • 1
    CodeIgniter does that for you. Just add this mime to your mimes.php and carry on. :) – Phil Sturgeon Feb 27 '11 at 10:58
  • I done it but the problem is if user upload .EXE CI will allow it. Anyway I coded it to check for EXTENSION after uploading. If it's not TTF, unlink file. Thx for all answers. :) – CappY Feb 27 '11 at 11:10
  • I have a similar problem and it has to do with your magic.mime file. That file is probably out of date just like it is here, but I don't know of an easy way to update or expand the file with new file types such as TTF. Similar thread here: http://stackoverflow.com/questions/6184220/safely-allow-upload-of-web-fonts-ttf-eot-svg-woff-otf-via-mime-types-fil – Jorre Jun 03 '11 at 07:23