9

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 and the mime-type the browser and the php report is application/octet-stream instead of application/excel, application/vnd.ms-excel or application/msexcel for a xls file. Of course codeigniter upload plugin will report an error (invalid file type) as it tries to match the file extension with the mime-type.

The weird(est) thing might be that the same code worked for months and now stopped working with the latest Chrome (16.0.912.77), Firefox (10.0) and IE9.

Has anyone had the same (or similar) problem and care to share the solution?

Thank you very much. PS: I won't provide code as it's not really a code matter, but if necessary i'll upload some snippets.

EDIT

It might be relevant: the error doesn't happen with same browsers on a similar configuration, but with MS Office instead of Libre Office (on my pc). It doesn't happen on a GNU/Linux based + Libre Office system either. SO, could it be Windows playin' hard on the open source suite, or the Libre Office changing the mime-types just for the heck of it?

MJC
  • 3,871
  • 4
  • 23
  • 34

4 Answers4

9

I'm getting this error also.

CI is reporting a file type of 'application/zip' which makes sense as the xlsx format is a compressed format (rename it to zip and you can open the contents).

I have added/replaced the following line to the mime types file (application/config/mimes.php):

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

and that works (for this browser at least!)

Stevo
  • 2,601
  • 3
  • 24
  • 32
  • 1
    hmmm, it might solved the problem, but it would allow .zip files that are not xlsx files to be uploaded, if i'm not mistaken. – MJC Mar 13 '12 at 18:26
  • Yeah, I thought that initially, but CI does a check of file extension AND mime type, so if you try and upload a zip file, because the extension is zip and not xlsx it throws an error. I've also added the following lines to the mimes.php file for the work I've been doing: 'slk' => 'text/plain', 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/zip'), 'ods' => 'application/octet-stream – Stevo Mar 14 '12 at 07:54
  • 1
    what about a zip file with a xlsx extension (and not a valid xlsx file) ? – MJC Mar 15 '12 at 01:55
  • Nevertheless, your aproach is still better than no test at all, so I'll accept your answer. – MJC Mar 15 '12 at 01:55
  • That is of course where it falls down. However like you say it's better than nothing. And you could perform some additional checks to see if the data being uploaded is what you are expecting. – Stevo Mar 15 '12 at 15:55
  • Right now it's not that big of an issue, since the application is used by a limited number of "not so stupid" people. Once uploaded, the file is "processed" by PHPExcel. But in a more global website it would still be a little issue. Thank you for your help. – MJC Mar 15 '12 at 19:30
  • Snap! I'm using PHPExcel for processing. I've yet to do the 'additional checks' I mentioned... – Stevo Mar 16 '12 at 08:52
  • 1
    This is probably not related to CI at all. I am getting $_FILES['file_browser']['type'] as application/octet-stream when I uploaded a .xlxs file from IE 7 browser. So I added 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/octet-stream') in my code igniter's mime file – piyush Jan 23 '13 at 06:50
5

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!

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
2

Just for the records, I found the cause, mime-type was missing on the windows registry, solved adding these keys with a .reg file:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\.xls]
"Content Type"="application/excel"

[HKEY_CURRENT_USER\Software\Classes\.xlsx]
"Content Type"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

But would rather use the solutions above, I don't like to mess with the registry.

2

This was a CI bug a few months ago: https://github.com/EllisLab/CodeIgniter/issues/394 . mimes.php in the framework was updated and the bug was resolved. Update your CodeIgniter library to 2.1.0 (or newer).

Also a good thing to test/dump are your server mime types.

Another alternative is forcing the mime type. With .htaccess, that would be

AddType application/excel .xls .xlsx

For a whole debugging adventure, test various office files with get_mime_by_extension($file) with the File Helper (http://codeigniter.com/user_guide/helpers/file_helper.html)

Seabass
  • 1,963
  • 12
  • 15
  • Thank you for your answer. Unfortunately i am already using CI 2.1.0, so the problem can't (shouldn't) be that. I tested the $_FILES[$field]['tmp_name'] and it was also "application/octet-stream". The weirdest thing is that only happens on my machine. Later I tested in the same browsers and OS on another machine, (the only difference would be Libre Office (on my pc) and MS Office on the other) and it worked fine. Also tested in a linux based OS with Libre Office installed, and it worked nicely too. I will try the .htaccess aproach. Thanks you. – MJC Feb 06 '12 at 22:12
  • Well. Tried the .htaccess aproach. Nothing. I will probably give this issue a rest, as the application is for a internal use for a close client, and it's working ok for them. Anyway, I would love to find the cause of this problem, if not, i'll accept your answer. It might help other people. – MJC Feb 06 '12 at 22:38