Apache/PHP: $_FILES Array mysteriously empty

Frustrating moment of the day:

A file upload form that’s been working for a long time, suddenly fails. No file uploads work on the entire apache installation. Checking the drive space in the /tmp folder, even though 85MB are left, freeing up space reveals that if there isn’t a lot of free space left for /tmp, the $_FILES array just goes empty without further explanation. No useful error messages, or anything.

Freeing up space for /tmp solves the problem effectively.

Update: Since so many commenters have posted other problems that cause similar effects, here is a quick summary of potential issues that you should check:

  1. Check php.ini for file_uploads = On, post_max_size, and upload_max_file_size. Make sure you’re editing the correct php.ini – use phpinfo() to verify your settings. Make sure you don’t mispell the directives as 8MB instead of the expected 8M!
  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 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. Do not use javascript to disable your form file input field on form submission!

  4. Make sure your directory has read+write permissions set for the tmp and upload directories.
  5. Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.
  6. Make sure your file destination and tmp/upload directories do not have spaces in them.
  7. Make sure all FORMs on your page have /FORM close tags.
  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.

98 thoughts on “Apache/PHP: $_FILES Array mysteriously empty

  1. Amanuel, the exact same thing happened to me…

     enctype="multipart/form-data"
    

    …did not work, whereas…

     enctype='multipart/form-data'
    

    …(which is exactly the same except with single-quotes instead of double) did work. As far as I know it shouldn’t matter in HTML whether the attributes are in single or double quotes, so it makes me wonder if this is a bug. If it is a bug, is it in Firefox (I’m using 3.0.8), PHP (5.2.6) or Apache (2.2.9)?

  2. I also have the problem with the empty $_FILES array and the problem was that my script didn’t assign a “NAME” property to the INPUT field! doh!

  3. Rrrrgh!

    So far nothing in this post has helped, but I’ve yet to really delve into the php.ini or directory permissions leads.

    Any chance this could be something in an Apache config file?

  4. Finally realized that i had a fricking typo. “ENCYTYPE” instead of “ENCTYPE”!

  5. All good suggestions. I have a solution that has not been mentioned. However, I have had blank $ FILES array for more than 6 months, and I exhausted myself in an attempt to fix it. So humor me while I give you the outline.

    Check this out: 4 coders using linux. All host a project locally. All 4 coders get empty $_FILES array when testing locally. All Mandriva09 Apache2 PHP5 – One has Zend Community server. We have 3 other servers, 1 on the local network, 2 else where. File uploads work fine on all of the servers. Debian and RedHat.

    I began writing this to beg for help. I was at a loss after 6 months. JUST NOW I realized the problem.

    localhost. The $ FILES array is blank when I test from the same machine I am hosting on.

    I just verified this. All 4 development machines work fine. The config is correct, the forms are fine, I just have to hit it from another computer.

    Solution? No, it is not apparent to me how to solve this. I would like to understand why this happens. However for me, and most other developers, this just means a little annoyance when testing upload scripts.

    Please let post a reply if you can duplicate this issue. Provide your environment info so we can find a trend, and maybe a real solution.

    ~~KILLERKAMEL

  6. For me it was #$% Safari 4.0.3. The worst version yet of this #$% browser. Firefox works fine but Safari will return an empty $_FILES array. Maybe this will help someone else out there who is going crazy.

    I like Safari, but version 4.0 has broken A LOT of things.

  7. Wow, thanks! Problem for me was the enctype as well. Had an include that contained another form that appeared on the page and had to set its enctype as well.

    Spent hours before you guys solved this for me! Thanks!

  8. hi same problem here $_FILES works on windows with all php.ini settings max upload limit etc but never works when copied to ubuntu server thanks in advance sudeep

  9. If you’re finding that $_FILES[] array is empty after submitting the form with file input, then first make sure that you have the form enctype attribute is set to “multipart/form-data”.

    That is as below: …

    If enctype attribute is not set in the form, then it would take default value for it as “application/x-www-form-urlencoded”, which will make the form not send any file data to target program/PHP file.

    If $_FILES array remains empty even after setting enctype right, you should check against other settings in your PHP server to make sure it allows the server to upload files.

  10. alex :

    For me it was #$% Safari 4.0.3. The worst version yet of this #$% browser. Firefox works fine but Safari will return an empty $_FILES array. Maybe this will help someone else out there who is going crazy.

    I like Safari, but version 4.0 has broken A LOT of things.

    I’m on 4.04 (just updated) and I get an empty array after uploading 3 of 6 files (same source code) and I’m not running into php limits, the files are in KB’s not MBs…

    Haven’t seen in mentioned yet. Enter echo “

    "; printr($FILES);  in your source to be sure if you're array is empty or you just assume it is...

  11. Hey Guys!

    I figured out, that it does not work if you copy/paste the double quotation marks from here to your php-file. So be sure that you actually TYPE the quotation marks. In PuTTY there is a tiny optical difference between the copied and typed quotation marks.

    It’s now working with enctype=”multipart/form-data” (DON’T COPY QUOTATION MARKS)

    Thanks for the hints anyway! best regards flo

  12. Well, guys, I’ve had the same problem, trying vardump out of $FILES and getting an empty array… tried everything, my enctype was fine, my method was fine, but… turned out that my problem was with the freaking input file tag!! it was only with its id, not with its NAME, and name is the attribute that php looks out when sending info to $_FILES.

    thanks to Ayyasamy for the tip!

  13. I was too stupid not noticing “-” between form and data in the “multipart/form-date” tag.

  14. Yo, thank you, I made the mistake of disabling the file tag before posting to the server. I am experienced and I did not think of this at 1:30am in the morning. Great Job.

  15. Just thought I’d chime in – for whomever might run into this. Same problem , although Chrome would allow uploads , no other browsers would. The problems was “multipart/formdata” instead of “multipart/form-data”

    :)

    Thanks for the help!

  16. CAN’T BELIEVE IT

    I’m trying to figure out why the hell my $_FILES is empty. Permissions was okay, input tag was okay, quotes were typed, but I had “multipart/formdata” instead of “multipart/form-data”.

    BUT, notice that Google Chrome fixed it automatically!

  17. I was running into a problem with an Empty $FILES array too, everything seemed to be in order but it always displayed as empty. I was calling the $FILES variables after all of my $POST variables and that must be what screwed it up because moving the vardump($_FILES['file']['name']) to the very top of the script fixed it.

    I thought I was going mad, checking and re-checking my enctype over and over. :)

  18. Don’t know whether anyone else is still having this problem but I’m struggling to find the will to live with it… all I’m trying to do is upload a 4kb image so that rules out the max size on both POST and UPLOAD php flags (although I have check them anyway). I’ve checked my code a million times, I have the enctype right with double quotes… tried on every browser. STILL have an empty $_FILES array.

    Someone help?

  19. @Curtis same mistake here (5 years later). Set INPUT MAXFILESIZE to 5000… without realizing that was 5000 BYTES and not 5000 KBYTES! Argh!

  20. This may help someone….

    After doing a dump of $FILES using printf($FILES) I found that I was getting a single number. It then just dawned on me that it was an error code. I was getting the number 5, which was referring to PHP’s inability to find a temporary folder. What was weird was that I did a very basic HTML test on the same server as my WordPress site (which is giving me the problems) and it worked great. Now add exactly the same piece of code to a WordPress website running on another virtual server (which uses the same instance of PHP as my basic test) and *BOOM … it fails to upload.

    I am currently looking into why the temporary folder which works fine on the basic example, now no longer works with WordPress.

    I suspect that WordPress may have some settings which affect things, or it is a permissions issues with folders. Anybody got any ideas?

    Dave.

  21. Quick update on my last comment. I have been conducting some research and looks like it may be an AJAX issue, as I am submitting the form via AJAX (using JQuery) rather than the standard HTML post method.

    This has thrown up a few interesting points as (from what I can make out), this submission method using AJAX/Jquery means that the $FILES in redundant or with relevance to this discussion… $FILES is empty.

    If anyone knows something to the contrary about AJAX uploading, then I would be delighted to hear!

    Oh… BTW… the way I found this out because I decided to do a dump of the $POST variable array (via the printr($_POST) command).

    Hopefully this may help someone, even if it is just to trigger a thought ;o)

    Good luck!!!

    Dave

  22. There are limitless ways to fuck it up. Here add mine to the list – my field was not between the tags. Dumbass.

  23. man thanks for having given this useful piece of advice to me,I never thought that disabling my fileupload (using Js) would be the cause of an empty $_FILES array.

  24. I kept writing $FILES, leaving out the underscore, after i had done printr($FILES) at the top; so i had things printing out in a debug attempt some of the time and not in other places, effectively driving me mad. Was doing this today while sick, guess I shouldn’t program w/ a cold… >.<

  25. Hi guys !

    I just wanna bring my little rock on that (very nice) wall. I had an headhache with that problem too. You must know that the empty $FILES variable problem can also happen if the “maxfile_uploads” property isn’t set. (Default is 20)

    Good luck all ! Tom.

  26. Tried all of this and nothing worked… but my php.ini had

    maxfileuploads Off

    I updated it to

    maxfileuploads 20

    and was off to the races… Good luck everyone.

  27. Just wanted to say thank you! I’m new to uploading files with PHP, but seeing your checklist (specifically that the enctype attribute was required) saved the day! Thanks for your help!

  28. One question. I am getting an empty files array. I don’t see a tmp folder. Did everyone else have to create the tmp directory or should it be there in your APACHE web server?

  29. Greetings everyone, I tried every single advise in the post and comments, with no luck, though my problem is slightly different: I get an empty $FILES (vardump empty…) ONLY when the uploaded file exceeds some small number (aprox 3 KB) Not only empties $FILES, but also it empties EVERY input written above the input file in the HMTL (also vardumped $__POST)

    Any idea is welcome =(

    TIA

  30. Ayyasamy, on May 20th, 2008 at 02:43, has posted, that his problem was missin “name” attribute in input type=”file” tag. I’ve ran into exactly the same situation. I’m lucky it took just about 1 hour to resolve my problem. Thank you all.

    One more thing – but Im not sure if Im correct (like 50 / 50):

    uploaded file must be handled inside one script – if you redirect (like header(“Location: …”)), your temporary uploaded file will gone …

  31. Thank you! Thank you! Thank you! My problem was the enctype attribute. Had I not found this page who knows how long I would have spent trying to figure out what was going on.

  32. I was using JavaScript to check if the user was using the file upload functionality or not. I wanted to set the enctype via JavaScript to either “multipart/form-data” or “application/x-www-form-urlencoded” depending.

    My form worked fine in FireFox, but not in IE. I found that changing the enctype via JS didn’t do anything in Internet Explorer. IE expects a different form attribute – ‘encoding’. So, if you’re setting the enctype via JS, you should cover your bases by setting both…

    if(fileUpload){ document.getElementById(‘formId’).enctype=”multipart/form-data”; document.getElementById(‘formId’).encoding=”multipart/form-data”; // For IE } else{ document.getElementById(‘formId’).enctype=”application/x-www-form-urlencoded”; document.getElementById(‘formId’).encoding=”application/x-www-form-urlencoded”; // For IE }

    For more info:

    http://www.talkphp.com/javascript-ajax-e4x/1176-form-enctype-internet-explorer.html http://www.bennadel.com/blog/1273-Setting-Form-EncType-Dynamically-To-Multipart-Form-Data-In-IE-Internet-Explorer-.htm

  33. So, I discovered a different issue that yielded the same empty array. The ‘name’ attribute must not contain chars like ‘[' or ']‘ or ‘{‘ or etc.

    First of all, php’s file upload handling leaves a lot to be desired (see the structure used when using multiple files).

    to me, this looks like a bug.

    Here is an example from python that shows what I expect php to do: [ { 'file["my{}stuff"].data’, ‘testfile2.txt’, ‘this is another test file (2)\n’ }, { ‘file["my{}stuff"].data’, ‘testfile1.txt’, ‘this is test file 1\n’ } ] ]

    Here is what php does: array(0) { }

    HTML for form:

    POST source: Content-Type: multipart/form-data; boundary=—————————141539390286251557952150290 Content-Length: 468 —————————–141539390286251557952150290 Content-Disposition: form-data; name=”file[\"my{}stuff\"].data”; filename=”testfile2.txt” Content-Type: text/plain this is another test file (2) —————————–141539390286251557952150290 Content-Disposition: form-data; name=”file[\"my{}stuff\"].data”; filename=”testfile1.txt” Content-Type: text/plain this is test file 1 —————————–141539390286251557952150290–

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">