CodeIgniter: "The filetype you are attempting to upload is not allowed."
When debugging file upload failures in CodeIgniter, the error message "The filetype you are attempting to upload is not allowed" often feels misleading. It suggests a configuration mismatch between your allowed_types and mimes arrays, yet the actual culprit can be as simple as the file extension itself.
In the scenario described, the developer has configured $config['allowed_types'] = 'avi|mpg|mpeg|wmv|jpg'. This allows uploads based on the file name extension. However, the mimes configuration maps extensions to MIME types. When uploading an AVI file works but a WMV file fails with the exact same error, despite the MIME type mapping for 'wmv' being defined in config/mimes.php, the root cause lies in how CodeIgniter validates the input before checking the extension list.
The validation logic inside the Upload library follows a specific order of operations:
- It checks if the file is uploaded successfully (
$_FILES['userfile']['error'] === UPLOAD_ERR_OK). - It verifies the MIME type against the
mimesarray only if the upload was successful. - It checks the file extension against
allowed_types.
The critical failure point here is that CodeIgniter's Upload library performs a strict check on the file extension present in $_FILES['name'] before consulting the MIME type mappings, but it does so based on the actual extension of the uploaded file, not just what you expect. If the client sends a file named wmv.wmv, the validation logic sees the extension .wmv. Since 'wmv' is in the allowed list, this should pass. However, there is a subtle behavior regarding how the library interprets the MIME type versus the extension when the upload path or other settings interact with the validation flow.
More commonly, this specific error arises when the allowed_types configuration restricts uploads to specific extensions, but the file being uploaded has an extension that isn't in that list, regardless of its actual MIME type. In the provided code snippet, the developer is relying on allowed_types. If a file arrives with an unexpected extensionâsuch as .avi working and .wmv failingâit implies that either the extension mapping is incomplete or the mimes array isn't being loaded correctly into the library instance.
To resolve this, ensure that the config/mimes.php file contains the correct mappings for all supported extensions. The provided snippet shows:
'wmv' => array('video/x-ms-wmv', 'audio/x-ms-wmv')
This is syntactically correct. However, the library will only use these mappings if they are explicitly loaded or if the allowed_types setting allows the extension to pass the initial filter.
A frequent oversight occurs when developers assume that defining a MIME type in config/mimes.php automatically enables that file type without also ensuring it is listed in $config['allowed_types']. In the example, 'wmv' is listed in both places, which suggests the configuration is technically sound. The discrepancy where AVI works but WMV does not on different machines points to a server-side environment issue rather than pure code logic.
Specifically, Apache and PHP often handle file uploads differently regarding MIME type detection. If the server's default mime.types file is missing an entry for .wmv, PHP might misidentify the file as having no recognized type or a generic text type. When CodeIgniter receives a file with a content type it doesn't understand, and the extension check fails silently or triggers a security bypass, the library defaults to rejecting it with the "filetype not allowed" error.
To fix this across different environments:
- Verify Server MIME Types: Run
php -i | findstr mime.types(Windows) orphpinfo()to ensure your server recognizes.wmv. If the server doesn't know what a.wmvfile is, PHP might return an empty string for$_FILES['userfile']['type'], causing validation to fail. - Explicitly Set MIME Types: If relying solely on extension matching causes issues across different clients or servers, you can bypass the MIME type check entirely by setting
$config['mimes'] = array()in your controller. This forces CodeIgniter to rely strictly on theallowed_typeslist (the file extension), which is generally more robust against server-side MIME database inconsistencies. - Check File Permissions: Ensure the
uploadsdirectory has write permissions for the web server user. While this usually results in a "Could not find the upload path" error, permission issues can sometimes manifest as silent failures depending on the PHP version and OS configuration.
For production environments where consistency is key, relying on file extensions via allowed_types is often safer than relying on MIME types detected by the server. If you need to enforce strict security policies regarding content, you should validate the MIME type using a library like mime_content_type() after uploading, rather than trusting the initial validation check which can vary based on server configuration.
If you are migrating from CodeIgniter 1.x to 2.x or 3.x, be aware that the Upload library behavior changed significantly. In modern versions, you should instantiate the upload library with a specific instance name and use $this->upload_data() for results, rather than the deprecated do_upload() method combined with display_errors. The logic remains similar, but the API surface has evolved to support better validation chains.