如何在codeigniter中创建自定义mime类型

I'm creating an upload form using CodeIgniter and I'm uploading a file with a .apk extension. in My controller for time I have used * which is working but I want to allow the only apk files.

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

can anyone help me to create custom mime type with sample code.

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

You can use this.

It will work fine.

thank you.

In the docs, allowed_types refers to

The mime types corresponding to the types of files you allow to be uploaded. Usually the file extension can be used as the mime type. Can be either an array or a pipe-separated string.

So you can use:

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

If you want to be more specific and check the actual MIME type, for apk this is

application/vnd.android.package-archive

I hope it's not too late to answer this question!!

I was implementing a similar thing and ran into the same issue! After a bit of research on stackoverflow, I have a tried and tested answer.

As @phobia82 correctly said, this is an issue of mime types. Having checked in some browsers and CodeIgniter 3.x, adding a custom mime type is what helps! Also, worth noting is the fact that apk files are recognized with 2 mime types (from what I have tested)

| application/vnd.android.package-archive |
|-----------------------------------------|
| application/java-archive                |

So, goto config/mimes.php and add this to the array

'apk'   =>  array('application/vnd.android.package-archive','application/java-archive')

This was the reason why my upload failed. Codeigniter detected the APK archive as a java archive and the Apache server detected this as vnd.android.package-archive. Once you are done with this, you can use

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

Hope this helps!!