Common Issues with CodeIgniter File Upload

In the last post I wrote about how to extend the File Upload Library to set disallowed file types rather than only the allowed file types. There was a fair amount of interest in file upload for CodeIgniter and so I’ve decided to post some tips for troubleshooting the most common issues I’ve come across.

“The filetype you are attempting to upload is not allowed”

Many times this is the appropriate error you would expect to see after a disallowed file type has been uploaded, however, I’ve noticed that there are a couple cases where files inaccurate fail. Here are some potential issues and their solutions.

  • Option ‘allowed_types’ Is Required – If no list of file types is provided in $config[‘allowed_types’], you will always get this error. The documentation shows the options with a default value of “none”, but it fails to make it obvious that it is a required input.
  • MIME Type Is Not Listed – Another potential solution to this problem could be that the extension is not listed in config/mime.php. Any file type that should be allowed must exist in the $mime array or it will fail.
  • MIME Type Variations – Different browsers often return different MIME types. For this reason, config/mimes.php accepts multiple MIME types for a given file type. Many times a variant of the main MIME type will need to be added to accommodate all browsers.

$data[‘file_path’] is an Absolute Path

The $config array accepts ‘file_path’ as either a relative or absolute path. It is definitely convenient to allow either as an input, however, the $data array returned after processing the file returns an absolute path. This can certainly cause some issues if the expectation is that the returned ‘file_path’ is in the same format as the ‘file_path’ provided in the $config array.

Accepting Multiple Files in a Form

There is an ongoing discussion in the CodeIgniter Forums on how to accept multi-file uploads. There are a handful of great solutions available and lots of people supporting the topic.