Why would $_FILES be empty when uploading files to PHP?
Here's a check-list for file uploading in PHP:
- 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 tophp.ini
. - Make sure
you’re editing the correct ini file –
use the
phpinfo()
function to verify your settings are actually being applied. - Also make sure you don’t
misspell the sizes - it should be
100M
not100MB
.
-
Make sure your
<form>
tag has theenctype="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. -
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[]">
-
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
. -
Make sure your file destination and tmp/upload directories do not have spaces in them.
-
Make sure all
<form>
's on your page have</form>
close tags. -
Make sure your FORM tag has
method="POST"
. GET requests do not support multipart/form-data uploads. -
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.
-
Make sure you are not using Javascript to disable your
<input type="file">
field on submission -
Make sure you're not nesting forms like
<form><form></form></form>
-
Check your HTML structure for invalid/overlapping tags like
<div><form></div></form>
-
Also make sure that the file you are uploading does not have any non-alphanumeric characters in it.
-
Once, I just spent hours trying to figure out why this was happening to me all of a sudden. It turned out that I had modified some of the PHP settings in
.htaccess
, and one of them (not sure which yet) was causing the upload to fail and$_FILES
to be empty. -
You could potentially try avoiding underscores (
_
) in thename=""
attribute of the<input>
tag -
Try uploading very small files to narrow down whether it's a file-size issue.
-
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.
-
Be sure that you're not submitting the form through an AJAX POST request instead of a normal POST request that causes a page to reload. I went through each and every point in the list above, and finally found out that the reason due to which my $_FILES variable was empty was that I was submitting the form using an AJAX POST request. I know that there are methods to upload files using ajax too, but this could be a valid reason why your $_FILES array is empty.
Source for some of these points:
https://web.archive.org/web/20050426084124/http://getluky.net/2004/10/04/apachephp-_files-array-mysteriously-empty/
As far as the HTML you appear to have set that part up correctly. You already have the enctype="multipart/form-data"
which is very important to have on the form.
As far as your php.ini
setup, sometimes on systems multiple php.ini
files exist. Be sure you are editing the correct one. I know you said you have configured your php.ini
file to have file uploads, but did you also set your upload_max_filesize
and post_max_size
to be larger than the file you are trying to upload? So you should have:
file_uploads = On; sounds like you already did this
post_max_size = 8M; change this higher if needed
upload_max_filesize = 8M; change this higher if needed
Does your directory: "c:\wamp\tmp"
have both read and write permissions? Did you remember to restart Apache after you made the php.ini
changes?
It is important to add enctype="multipart/form-data"
to your form, example
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
Thank you everybody for the vary comprehensive replies. Those are all very helpful. The answer turned out to be something very odd. It turns out that PHP 5.2.11 doesn't like the following:
post_max_size = 2G
or
post_max_size = 2048M
If I change it to 2047M
, the upload works.