Upload File Excel Problem on CodeIgniter 2.1.2

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') or $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;
$this->load->library('upload', $config);
$config['file_name'] = 'xxx';
$this->upload->initialize($config);
~~do image uploading here...then

$config['file_name'] = 'yyy';
$config['allowed_types'] = 'xlsx|xls|csv';
$this->upload->initialize($config);
~~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')

to this

'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 $this->upload->data() and print_r the $_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 (is_array($mime))
{
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))
{
return TRUE;
}
}

See. It’s really cheating. So, please… if you know better solution than above, please do not hesitate to leave comments.

_rd_

~Featured image get from bezambee.com
~Emoticon stealed from kaskus.co.id

Advertisements

8 thoughts on “Upload File Excel Problem on CodeIgniter 2.1.2

  1. I use codigniter 2.4 version . When I upload excel file with CI,I am ok. as you say, i changed only mime types .it is ok to upload file. thank you…

    1. I don’t know, the mime-type of .xls type somehow broken (especially from non ms.Excel application), not the appropriate one.

      There’s another way to upload. With ajax if you don’t mind with html5. Maybe I’ll write about it later 🙂

  2. I’m having the same problem while trying to upload spreadsheets with CodeIgniter. This has not been solved in the latest CI version. The spreadsheets are not corrupt. They just had been generated (I guess with PHPExcel) in some way the mime sh…. can’t recognize it correctly. I hope I could force the upload anyway, but got no luck, just had to set $config[“allowed_types”] = ‘*’ for the upload to work, which is not good and forces me to write more code to check file extensions and redirect user to an error page, etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s