CodeIgniter图片上传报错:提交表单允许的最大文件大小超出
Hey there, let's break down why you're hitting this upload error even though you've set $config['max_size'] = '0' in your CodeIgniter setup. Here are the key fixes to try:
1. Check your HTML form's MAX_FILE_SIZE hidden field
First off, the error message specifically calls out the submission form, so let's start there. Even if your CodeIgniter config allows large files, if your HTML form has a MAX_FILE_SIZE input set too low, it'll block the upload before it even reaches CodeIgniter.
Add or update this hidden input in your upload form (set the value to a byte count matching your desired max size—for example, 67108864 bytes = 64MB):
<input type="hidden" name="MAX_FILE_SIZE" value="67108864">
Make sure this input comes before your file upload field in the form.
2. Adjust PHP's core upload limits
CodeIgniter's max_size = 0 means it won't enforce its own size limit, but PHP itself has two critical settings that still cap uploads:
upload_max_filesize: The maximum size of a single uploaded filepost_max_size: The maximum size of the entire POST request (this needs to be larger thanupload_max_filesize, since the request includes the file plus other form data)
How to update these settings:
Option A: Edit php.ini
Find your php.ini file (run phpinfo() in a test script to locate it) and set values like:
upload_max_filesize = 64M post_max_size = 70M
After saving, restart your web server (Apache/Nginx) for changes to take effect.
Option B: Use .htaccess (if you can't edit php.ini)
If you're on shared hosting, add these lines to your project's root .htaccess file:
php_value upload_max_filesize 64M php_value post_max_size 70M
Option C: Try ini_set (note: may not work for all hosts)
Some PHP settings can't be changed at runtime, but you can test adding this at the top of your controller:
ini_set('upload_max_filesize', '64M'); ini_set('post_max_size', '70M');
Keep in mind this won't work for all server configurations, so .htaccess or php.ini edits are more reliable.
3. Verify CodeIgniter's upload config is applied correctly
Double-check that your config isn't being overwritten somewhere. After loading the upload library, dump the active config to confirm:
$this->load->library('upload', $config); var_dump($this->upload->get_config());
Look for the max_size value—if it's not 0, something else is overriding your config (like a different config file or later code changes).
Why does this work in non-PHP apps?
Non-PHP web apps often use alternative upload mechanisms (like direct cloud storage uploads via JavaScript) that bypass PHP's core upload limits. Since your CodeIgniter app relies on PHP's built-in file handling, it's subject to these server-level constraints.
内容的提问来源于stack exchange,提问作者user8207090

