CodeIgniter File Upload and Mime Types
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:
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.
1 | array('text/plain', 'application/octet-stream', 'video/subtitle', '') |
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:
1 2 3 4 | if ( ! $this->upload->do_upload("subtitleFile")) $this->data["error"] = $this->upload->display_errors() . 'File Type: ' . $this->upload->file_type; else // File is correct, so do the normal processing |
if ( ! $this->upload->do_upload("subtitleFile")) $this->data["error"] = $this->upload->display_errors() . 'File Type: ' . $this->upload->file_type; else // 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: CodeIgniter, Mime Type, PHP, Upload
This entry was posted
on Tuesday, September 4th, 2012 at 2:04 pm and is filed under PHP.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Feedback & Comments
No Responses