CodeIgniter File Upload and Mime Types

By Akbar

As I mentioned in my other post CodeIgniter and File Upload Library, uploading a file using CodeIgniter requires you to also know the mime type of file being uploaded and add it to proper location.

I’m using CodeIngiter framework for Dynamic Subtitle Translator application, and it had worked great for me. However, few of my user complained that they can’t upload the sub-title files on the server, and get the following error when uploading subtitle files for translation:

The filetype you are attempting to upload is not allowed

I know that it has to do with not allowed mime-type, but I had no clue what mime-type was being passed by the uploader machine. To be on safe side, I searched for all the mime types which are used for the “.srt” file and added them to allowed list i.e.

array('text/plain', 'application/octet-stream', 'video/subtitle', '')

But one of the user was still getting the problem. Frustrated by not finding the clue, I started looking at the source code for the CodeIgniter upload helper library and found that the uploaded file type is stored in the “file_type” property of the class. That’s all what I needed. So, I changed the error message to also show the file-type of the uploaded application. Something like:

if ( ! $this->upload->do_upload("subtitleFile"))
	$this->data["error"] = $this->upload->display_errors() . 'File Type: ' . $this->upload->file_type;
	// File is correct, so do the normal processing

Once the file-type is displayed in error message, user can easily report the new mime-types not being supported, and I can add them back in supported list. A bit long and tiring process, but it works without changing or breaking anything at CodeIgniter inner level.

Tags: , , ,