I’m really desperate with this one. Maybe you have the same problem with me? Ok, let’s digging.
As you know, CodeIgniter has it’s own library for uploading files, named File Uploading Class and you may call the library on the Controller with
$this->load->library('upload', $config) with predefined
$config. Or you can just autoload it on the config as well.
Using CodeIgniter 2.1.2, I found several bugs for this Class. Bugs? I’m not sure if this is a bug or I just being dumb. So, this annoying bug is about uploading Excel formatted file (.xls, .xlsx).
This is my code on the Controller
$config['allowed_types'] = 'gif|jpeg|jpg|png';
$config['max_size'] = '350';
$config['upload_path'] = './uploads/images';
$config['overwrite'] = TRUE;
$config['file_name'] = 'xxx';
~~do image uploading here...then
$config['file_name'] = 'yyy';
$config['allowed_types'] = 'xlsx|xls|csv';
~~do the excel file uploading here...
Open the web, upload the excel file (yes, I’m really sure I upload the ‘.xls’ file, with size around 50kB), and tada… this error came out gently
The filetype you are attempting to upload is not allowed.
… rescan my code… feels like there is nothing wrong with it. Oh yeah, I do upload the image file and it’s running well.
So, as the Google-enabled-programmer I google about this. And googling about CodeIgniter 2.x is quite difficult, but I often find the result for CodeIgniter 1.7.x. At least, I found 2-3 solutions for this (I said solution because many of them said it works), but they all said about mime types.
So, as they said I change the mimes.php on the application/config/ folder. Changing the line number 32 from
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel')
'xls' => array('application/octet-stream', 'application/excel', 'application/vnd.ms-excel', 'application/msexcel')
As you can see, I just add the ‘application/octet-stream’ type for ‘xls’, because somehow ‘xls’ mimy-type read as it……. and then, it’s still failed, with the same error message .
I’m not giving up. Haha. Ok, try to see the consists of upload data with
$_FILES and find this file_type from upload data “cdf v2 document, corrupt: can’t expand summary_info”. I don’t know it’s mean. Just knowing that it’s not as suppose to be one of the array mime type above. So, then I edit the Upload.php on the system file. Please, don’t try to do this. It’s really cheating and danger. But, it works. Hahaha. on function is_allowed_filetype() line 584, I insert this code on line 624.
if ($this->file_type == "cdf v2 document, corrupt: can't expand summary_info")
$this->file_type = "application/octet-stream";
if (in_array($this->file_type, $mime, TRUE))
See. It’s really cheating. So, please… if you know better solution than above, please do not hesitate to leave comments.