Upload xls or xlsx files with codeigniter, mime-type error

后端 未结 4 1362
我寻月下人不归
我寻月下人不归 2020-12-03 15:20

Well, i believe this is not a Codeigniter problem per se as it is more of a mime-type.

I\'m trying to upload a file, a xls (or xlsx) file a

4条回答
  •  悲&欢浪女
    2020-12-03 15:57

    Please, go through the following Description and the hint and get the Answer easily!

    Description:

    Actually as many ones have advised to add/replace the following line in the file (application/config/mimes.php):

    'xlsx'  =>  array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), 
    


    But I have realized that in CodeIgniter Version 2.2.* the issue is little bit different! They have added that line already, but forgot to add the following "file_type" ==> 'application/vnd.ms-excel'

    'xlsx'  =>  array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel'),
    

    So adding the above 'application/vnd.ms-excel' into the array of xlsx file type, let me upload the .xlsx files!

    Hint:

    Whenever you get the following error, on CodeIgniter Platform, and uploading files:

    The filetype you are attempting to upload is not allowed.

    Do the following in your Controller's upload method,

    var_dump($this->upload->data());
    

    And it will give you a huge array which you can get an idea from this link.(Please, see the end of that page). In that array you can get what's the real mime_type of the file you are trying to upload but not giving you to upload.

    Answer:

    In my case, my file extension was, .xlsx , and the mime type was application/vnd.ms-excel , which was not added into the

    'xlsx'  =>  array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'), 
    

    So I added it manually, and after that it works VERRY WELL!!!

    Same thing happened to uploading CSV once again, when I checked the file extension is .csv but the mime type was text/plain, when I added it to the following line:

    'csv'   =>  array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel');
    

    and saved as follows,

    'csv'   =>  array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'),
    

    It works like a charm! :D Try it, if you find something new within the above steps, please comment here!!! So, hoping this will be helpful to all of the CodeIgniter community, I posted it taking some time!

    Best Regards guys,

    Randika

提交回复
热议问题