Blog Archive

Monday 7 January 2013

Codeigniter Upload class and mime types

It's been a while since I last used Codeigniter's Upload class, and recently, whilst testing an upload form that used it, I found some file types were receiving the message:

The filetype you are attempting to upload is not allowed.

A perfectly reasonable error in some cases, but I was trying to upload Word file types like .doc and .docx, as well as rich text format (.rtf), and all these types were defined in config/mimes.php

Or so it seemed. Looking at the Upload class in Codeigniter, there is a method is_allowed_filetype() that attempts to match the uploaded file with an entry in mimes.php. This is where it would be possible to add a log_message() call to determine what the issue was.

The version of Codeigniter in the install I was using was 2.1 (it was a FUEL CMS application) and I had noticed that the very latest CI (2.13) had a slightly modified Upload class. Rather than replace the class wholesale, I created a MY_Upload class in application/lbraries, extending the Upload class with a new is_allowed_filetype() method copied from v2.13, with added log_message() entries to print the file type and expected mime type, particularly before the last return so that the ultimate issue was recorded.

Doing so revealed the vagaries of browser mime type detection. Sometimes, for reasons I'm yet to understand, the Upload class was not receiving a particular mime type (as expected in mimes.php) but the browser default 'application/octet-stream'.

So the solution was simple, add that mime to file types in mimes.php that are generating an otherwise unreasonable

The filetype you are attempting to upload is not allowed.

How many types need this entry added to will be determined by your needs and or testing regime. I tested the upload on different browsers, on different OS's, and on local and live servers and found several different behaviours even with the same file.

No comments:

Post a Comment

My top artists