How to upload apk file using CodeIgniter?

“How to upload apk file using CodeIgniter?” is the most asked question now a days on forums. Today, we are going to cover the issue of not uploading apk using CodeIgniter.

What is mime type?

Mime type is nothing but file types (not file extension) like image, audio, video, compressed files etc. You can find exact definition on internet easily. CodeIgniter having inbuilt set of mime types and allowed extension configuration inside Config folder. Android install-able files are not in the set of allowed types in CodeIgniter configuration.
You can check this path for mime types in Codeigniter:

project-root/application/config/mimes.php

How to introduce new mime type?

APK file extension is not normal file extension. It is not expected to upload apk files other than Google Play Store. So CodeIgniter don’t have the file extension in its allowed file configuration. Moving toward the path and open the file mimes.php

project-root/application/config/mimes.php

Add an entry into the array at the end. As our .apk extension is not found in the mimes.php file, we will add the extension and its mime-type into the array.

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

Basically for APK files, ‘application/vnd.android.package-archive’ is the mime-type, but for CodeIgniter, it is considering it as ‘application/zip’, so we need to add this in the mimes.php

How to upload apk file using CodeIgniter?

Our file upload code lies in controller itself. So, in the controller you have to mention following configuration for file upload.

public function file_upload()
{
    $this->load->helper(array('form', 'url'));
     
    $config['upload_path']          = './upload/';
    $config['allowed_types']        = 'apk|APK';
    $config['max_size']             = 100000;

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('apkfile'))
    {
      $error = array('error' => $this->upload->display_errors());
      print_r($error);
    }
    else
    {
      echo "success";
    }
}

If you don’t mentioned the mime type then it will show the error as follows:

Array ( [error] =>
  The filetype you are attempting to upload is not allowed.
)

 

[Image : Image is just for demonstration purpose. Code doesn’t product same UI]