Upload File Failing: Check List
Sometimes the upload file type just doesn’t work as expected in your PHP/HTML forms. I literally hate debugging that stuff and I’m sure you do as well. So I’d like to share a list of points to verify when this happens. Here is my checklist to help you debug your upload forms:
1 – Check php.ini for:
- file_uploads = On
- post_max_size = 100M
- upload_max_filesize = 100M
You might need to use .htaccess or .user.ini if you are on shared hosting and don’t have access to php.ini.
Make sure you’re editing the correct ini file – use the phpinfo() function to verify your settings are actually being applied. You can also use:
echo 'post_max_size = ' . ini_get('post_max_size') . "\n"; echo 'upload_max_filesize = ' . ini_get('upload_max_filesize') . "\n"; echo 'file_uploads = ' . ini_get('file_uploads') . "\n"; die;
Also make sure you don’t misspell the sizes – it should be 100M not 100MB.
2 – Make sure your <form> tag has the enctype=”multipart/form-data” attribute. No other tag will work, it has to be your FORM tag. Double check that it is spelled correctly. Double check that multipart/form-data is surrounded by STRAIGHT QUOTES, not smart quotes pasted in from Word OR from a website blog (WordPress converts straight quotes to angle quotes!). If you have multiple forms on the page, make sure they both have this attribute. Type them in manually, or try straight single quotes typed in manually.
3 – Make sure you do not have two input file fields with the same name attribute. If you need to support multiple, put square brackets at the end of the name:
<input type="file" name="files"> <input type="file" name="files">
4 – Make sure your tmp and upload directories have the correct read+write permissions set. The temporary upload folder is specified in PHP settings as upload_tmp_dir.
5 – Make sure your file destination and tmp/upload directories do not have spaces in them.
6 – Make sure all <form>‘s on your page have </form> close tags.
7 – Make sure your FORM tag has method=”POST”. GET requests do not support multipart/form-data uploads.
8 – Make sure your file input tag has a NAME attribute. An ID attribute is NOT sufficient! ID attributes are for use in the DOM, not for POST payloads.
10 – Make sure you’re not nesting forms like <form><form></form></form>
11 – Check your HTML structure for invalid/overlapping tags like <div><form></div></form>
12 – Also make sure that the file you are uploading does not have any non-alphanumeric characters in it.
13 – You could potentially try avoiding underscores (_) in the name=”” attribute of the <input> tag
14 – Try uploading very small files to narrow down whether it’s a file-size issue.
15 – Check your available disk space. Although very rare, it is mentioned in this PHP Manual page comment:
If the $_FILES array suddenly goes mysteriously empty, even though your form seems correct, you should check the disk space available for your temporary folder partition. In my installation, all file uploads failed without warning. After much gnashing of teeth, I tried freeing up additional space, after which file uploads suddenly worked again.
I hope that helps!
Inspired by http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/