0

I'm having a problem uploading ttf file to the server's upload folder, my code is working with otf, eot, woff formats, but not working with ttf. I've added the mime type in application/config/mimes.php

'ttf'   =>  array('font/sfnt', 'font/truetype', 'font/ttf', 'application/x-font-truetype', 'application/x-font-ttf', 'application/octet-stream'),
'otf'   =>  array('application/vnd.oasis.opendocument.formula-template', 'application/vnd.ms-opentype'),
'woff'  =>  'font/woff',
'eot'   =>  'application/vnd.ms-fontobject',

This is my upload class config

$config['allowed_types'] = 'ttf|otf|eot|woff';

I've checked all these one by one and together, but still not working, any help will be appreciated. thanks

Tayyab Hayat
  • 815
  • 1
  • 9
  • 22
  • can someone please help me out with this issue? – Tayyab Hayat May 26 '21 at 07:27
  • With full debugging turned on, is there an error either in your client-side developer console, or on the server? Do the files get stuck in the `/tmp` folder? – Chris Haas May 27 '21 at 15:10
  • No, it only return this error `The filetype you are attempting to upload is not allowed` – Tayyab Hayat May 28 '21 at 04:27
  • I've never used CI before, but some people are saying that one [specific version has a known issue](https://stackoverflow.com/a/9273986/231316) and also that you might have to [deeply inspect the mime type](https://stackoverflow.com/a/10850763/231316). I don't know if CI uses the browsers mime type (which is spoofable and different between browsers and file types) or the server, so you might have to troubleshoot on both ends. – Chris Haas May 28 '21 at 14:25
  • What .ttf files did you try? – Olivier May 30 '21 at 09:20
  • @Olivier i've tried truetype and sfnt both, but it is not working. – Tayyab Hayat May 31 '21 at 06:30
  • @ChrisHaas I've checked file type using git bash and add that mime type in codeigniter config file, every other font type is working in this way, but .ttf files are not uploading. – Tayyab Hayat May 31 '21 at 06:32
  • Just debug CI code and you will find out what is happening. – Olivier May 31 '21 at 07:49

2 Answers2

1

In codeigniter 2 and 3 there are some multiple bugs about mime typing and allowed file types... Try another upload method without codeigniter upload library or try

$config['allowed_types'] = '*';

also you can check allowed types first then upload it

$uploaded_file_extention = 'ttf'; //fetch the file extention first...
$allowed_types = array('ttf', 'otf', 'woff', 'eot');

if( !in_array($uploaded_file_extention, $allowed_types) ) {
    //do not continue to upload...
} else {
    //continue to upload with $config['allowed_types'] = '*';
}
Berk Kanburlar
  • 260
  • 2
  • 11
  • `'*'` is not good for security reasons, I've to use Codeigniter's upload library for some reasons. – Tayyab Hayat Jun 01 '21 at 06:45
  • yes you are true... but there is no other option i guess :( – Berk Kanburlar Jun 01 '21 at 07:28
  • unless you can put additional control on allowed types. You can use ci upload class but you can seperate allowed types controlling. Example; you can check type of file first after you can upload... Maybe this could help – Berk Kanburlar Jun 01 '21 at 07:31
0

Finally, I've got a solution, well I don't know that it was a CodeIgniter bug or my server is restricting me to upload these files. this code solve my issue

$mimetype = $_FILES['fontfile']['type'];
if(in_array($mimetype, array('font/sfnt', 'font/truetype', 'font/ttf', 'application/x-font-truetype', 'application/x-font-ttf', 'application/octet-stream'))) {
    return true;
} else {
    return false;
}

on true, you can call the CodeIgniter upload function with:

$config['allowed_types'] = '*';

Anyone looking for a solution for both PHP and CodeIgniter, here is the solution.

Thanks to @berk-kanburlar for hint

Tayyab Hayat
  • 815
  • 1
  • 9
  • 22