Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


Another possible culprit is apache redirects. In my case I had apache's httpd.conf set up to redirect certain pages on our site to http versions, and other pages to https versions of the page, if they weren't already. The page on which I had a form with a file input was one of the pages configured to force ssl, but the page designated as the action of the form was configured to be http. So the page would submit the upload to the ssl version of the action page, but apache was redirecting it to the http version of the page and the post data, including the uploaded file was lost.

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


Here's a check-list for file uploading in PHP:

Also make sure your form element containing the file input is NOT a child of another form element. e.g. <form><form><input type="file"></form></form>

Maybe the "accepted" answer solved the original post, but this answer is the one that I found most helpful. When in doubt, look at the source as seen by the browser. Checking off each item in this list and tracing backward, I found my error in a most unexpected place. If you're struggling with a similar problem, believe me, it's probably not a bug in Apache. ;)

The #7 is the common one with me

This doesn't apply to the discussion starter, but I had hosting provider who had their firewall policy too strict which caused some uploads to fail.

wow! thank you for this list. my problem was #2. i was calling $('#my-form')[0].reset(); in the submit handler.

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


2047M
post_max_size = 2048M
post_max_size = 2G

Not too large any more. We have clients who upload files in the 1-3G range quite regularly. Since they are uploading files onto their own servers, and they are IP whitelisted servers, the exchange is quite normal and merely a way to allow a client to use their equipment the way they want to use it. They pay the bills, no security risk involved, no problems.

Note that such a high value is an vulnerability to out-of-space/ddos attacks. Just adding this so people are aware its too much, when they try to copy&paste your solution. Anyways, 2 gigs would require an upload time that is way too long.

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:

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


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

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:

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?

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


<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>
enctype="multipart/form-data"
Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


Here another cause I found: When using JQuery Mobile and the form attribute data-ajax is set to true, the FILES array will be empty. So set data-ajax to false.

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


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

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:

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?

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


2047M
post_max_size = 2048M
post_max_size = 2G

Not too large any more. We have clients who upload files in the 1-3G range quite regularly. Since they are uploading files onto their own servers, and they are IP whitelisted servers, the exchange is quite normal and merely a way to allow a client to use their equipment the way they want to use it. They pay the bills, no security risk involved, no problems.

Note that such a high value is an vulnerability to out-of-space/ddos attacks. Just adding this so people are aware its too much, when they try to copy&paste your solution. Anyways, 2 gigs would require an upload time that is way too long.

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:

Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


<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>
enctype="multipart/form-data"
Note
Rectangle 27 0

apache Why would $_FILES be empty when uploading files to PHP?


max_file_uploads

If you are trying to upload an array of files then you may need to increase max_file_uploads in php.ini which is by default set to 20

Note