CodeIgniter and File Upload Library

By Akbar

CodeIgniter is a compact and amazing framework for developing neat and clear PHP projects. The more I work in this, the more I’m falling in love with it. Though it’s very well documented, but you can’t expect to find all what you need in the help file.

I was working on a project where I do need to support the subtitle file upload (of type SRT and SUB). According to the documentation all I need to do to support this is add the following line:

$config['allowed_types'] = 'sub|srt';

But when I add this line, the CodeIgniter was reject the SRT and SUB files with error that file type upload is not supported. As the framework is open source, thank God, so I looked at the corresponding code, and found that during upload, the code also checks for the file mime types and reject the file if the corresponding mime type is not defined in the “/application/config/mimes.php”. Once I know this, fixing this was just a matter of minutes. All I did was added the following two lines to the $mimes array in the file and it worked flawless:

'srt' => array('text/plain', 'application/octet-stream'),
'sub' => array('text/plain', 'application/octet-stream'),

The reason I added both the “text/plain” and “application/octet-stream” is because, when uploading the file, I sends the mime type as “text/plain”, while chrome and other browsers send “application/octet-stream”. Browsers! Browsers! Browsers!

Tags: , ,